@zenmux/rosetta-ai 0.0.8 → 0.0.10
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.
|
@@ -21,6 +21,10 @@ export declare class MessagesToGeminiConverter {
|
|
|
21
21
|
private parseGeminiThoughtSignatureBlock;
|
|
22
22
|
private emitGeminiThoughtSignatureBlock;
|
|
23
23
|
private generateId;
|
|
24
|
+
private generateToolUseId;
|
|
25
|
+
private getFunctionCallRawKey;
|
|
26
|
+
private getStreamToolUseId;
|
|
27
|
+
private collectToolUseNames;
|
|
24
28
|
private createStreamState;
|
|
25
29
|
private transitionBlock;
|
|
26
30
|
}
|
package/dist/messages/gemini.js
CHANGED
|
@@ -12,6 +12,7 @@ class MessagesToGeminiConverter {
|
|
|
12
12
|
convertRequest(params) {
|
|
13
13
|
const systemParts = [];
|
|
14
14
|
const contents = [];
|
|
15
|
+
const toolUseNames = this.collectToolUseNames(params.messages);
|
|
15
16
|
if (params.system) {
|
|
16
17
|
if (typeof params.system === "string") {
|
|
17
18
|
systemParts.push({ text: params.system });
|
|
@@ -24,7 +25,7 @@ class MessagesToGeminiConverter {
|
|
|
24
25
|
}
|
|
25
26
|
for (const msg of params.messages) {
|
|
26
27
|
if (msg.role === "user") {
|
|
27
|
-
this.convertUserMessage(contents, msg);
|
|
28
|
+
this.convertUserMessage(contents, msg, toolUseNames);
|
|
28
29
|
}
|
|
29
30
|
else if (msg.role === "assistant") {
|
|
30
31
|
this.convertAssistantMessage(contents, msg);
|
|
@@ -110,7 +111,7 @@ class MessagesToGeminiConverter {
|
|
|
110
111
|
for (const part of parts) {
|
|
111
112
|
if (part.functionCall) {
|
|
112
113
|
const fc = part.functionCall;
|
|
113
|
-
const id =
|
|
114
|
+
const id = this.generateToolUseId();
|
|
114
115
|
if (part.thoughtSignature) {
|
|
115
116
|
content.push(this.createGeminiThoughtSignatureBlock(id, part.thoughtSignature));
|
|
116
117
|
}
|
|
@@ -238,6 +239,7 @@ class MessagesToGeminiConverter {
|
|
|
238
239
|
},
|
|
239
240
|
});
|
|
240
241
|
}
|
|
242
|
+
const functionCallOccurrences = new Map();
|
|
241
243
|
for (const part of parts) {
|
|
242
244
|
if (part.thought && part.text) {
|
|
243
245
|
if (state.currentBlockType !== "thinking") {
|
|
@@ -256,7 +258,10 @@ class MessagesToGeminiConverter {
|
|
|
256
258
|
}
|
|
257
259
|
else if (part.functionCall) {
|
|
258
260
|
const fc = part.functionCall;
|
|
259
|
-
const
|
|
261
|
+
const rawKey = this.getFunctionCallRawKey(fc);
|
|
262
|
+
const occurrence = functionCallOccurrences.get(rawKey) ?? 0;
|
|
263
|
+
functionCallOccurrences.set(rawKey, occurrence + 1);
|
|
264
|
+
const fcId = this.getStreamToolUseId(state, fc, occurrence);
|
|
260
265
|
if (!state.seenFunctionCallIds.has(fcId)) {
|
|
261
266
|
state.seenFunctionCallIds.add(fcId);
|
|
262
267
|
if (part.thoughtSignature) {
|
|
@@ -338,7 +343,7 @@ class MessagesToGeminiConverter {
|
|
|
338
343
|
return events;
|
|
339
344
|
}
|
|
340
345
|
// --- Private: request helpers ---
|
|
341
|
-
convertUserMessage(contents, msg) {
|
|
346
|
+
convertUserMessage(contents, msg, toolUseNames) {
|
|
342
347
|
if (typeof msg.content === "string") {
|
|
343
348
|
contents.push({ role: "user", parts: [{ text: msg.content }] });
|
|
344
349
|
return;
|
|
@@ -373,7 +378,7 @@ class MessagesToGeminiConverter {
|
|
|
373
378
|
toolResponseParts.push({
|
|
374
379
|
functionResponse: {
|
|
375
380
|
id: tr.tool_use_id,
|
|
376
|
-
name: tr.tool_use_id,
|
|
381
|
+
name: toolUseNames.get(tr.tool_use_id) || tr.tool_use_id,
|
|
377
382
|
response: { output },
|
|
378
383
|
},
|
|
379
384
|
});
|
|
@@ -609,6 +614,35 @@ class MessagesToGeminiConverter {
|
|
|
609
614
|
generateId() {
|
|
610
615
|
return Math.random().toString(36).substring(2, 15);
|
|
611
616
|
}
|
|
617
|
+
generateToolUseId() {
|
|
618
|
+
return `toolu_${Date.now()}_${this.generateId()}`;
|
|
619
|
+
}
|
|
620
|
+
getFunctionCallRawKey(fc) {
|
|
621
|
+
return fc.id ?? fc.name ?? "__missing_function_call_id__";
|
|
622
|
+
}
|
|
623
|
+
getStreamToolUseId(state, fc, occurrence) {
|
|
624
|
+
const key = `${this.getFunctionCallRawKey(fc)}:${occurrence}`;
|
|
625
|
+
let id = state.streamFunctionCallIds.get(key);
|
|
626
|
+
if (!id) {
|
|
627
|
+
id = this.generateToolUseId();
|
|
628
|
+
state.streamFunctionCallIds.set(key, id);
|
|
629
|
+
}
|
|
630
|
+
return id;
|
|
631
|
+
}
|
|
632
|
+
collectToolUseNames(messages) {
|
|
633
|
+
return messages.reduce((toolUseNames, msg) => {
|
|
634
|
+
if (msg.role !== "assistant" || !Array.isArray(msg.content)) {
|
|
635
|
+
return toolUseNames;
|
|
636
|
+
}
|
|
637
|
+
for (const block of msg.content) {
|
|
638
|
+
if (block.type !== "tool_use")
|
|
639
|
+
continue;
|
|
640
|
+
const tu = block;
|
|
641
|
+
toolUseNames.set(tu.id, tu.name);
|
|
642
|
+
}
|
|
643
|
+
return toolUseNames;
|
|
644
|
+
}, new Map());
|
|
645
|
+
}
|
|
612
646
|
// --- Private: stream helpers ---
|
|
613
647
|
createStreamState() {
|
|
614
648
|
return {
|
|
@@ -620,6 +654,7 @@ class MessagesToGeminiConverter {
|
|
|
620
654
|
prevText: "",
|
|
621
655
|
prevThought: "",
|
|
622
656
|
seenFunctionCallIds: new Set(),
|
|
657
|
+
streamFunctionCallIds: new Map(),
|
|
623
658
|
toolCallBlockIndices: new Map(),
|
|
624
659
|
};
|
|
625
660
|
}
|
|
@@ -7,6 +7,7 @@ export declare class MessagesToResponsesConverter {
|
|
|
7
7
|
constructor();
|
|
8
8
|
convertRequest(params: Anthropic.MessageCreateParams): OpenAI.Responses.ResponseCreateParams;
|
|
9
9
|
convertResponse(response: RespResponse): Anthropic.Message;
|
|
10
|
+
private buildUsage;
|
|
10
11
|
convertStream(stream: AsyncIterable<RespStreamEvent>): AsyncIterable<Anthropic.RawMessageStreamEvent>;
|
|
11
12
|
convertStreamEvent(event: RespStreamEvent): Anthropic.RawMessageStreamEvent[];
|
|
12
13
|
private convertMessages;
|
|
@@ -119,7 +119,7 @@ class MessagesToResponsesConverter {
|
|
|
119
119
|
content.push({ type: "text", text: "", citations: null });
|
|
120
120
|
}
|
|
121
121
|
const stopReason = this.statusToStopReason(response.status, response.output);
|
|
122
|
-
const usage = response.usage;
|
|
122
|
+
const usage = this.buildUsage(response.usage, webSearchCount);
|
|
123
123
|
return {
|
|
124
124
|
id: response.id,
|
|
125
125
|
type: "message",
|
|
@@ -128,21 +128,43 @@ class MessagesToResponsesConverter {
|
|
|
128
128
|
content,
|
|
129
129
|
stop_reason: stopReason,
|
|
130
130
|
stop_sequence: null,
|
|
131
|
-
usage
|
|
132
|
-
input_tokens: usage?.input_tokens ?? 0,
|
|
133
|
-
output_tokens: usage?.output_tokens ?? 0,
|
|
134
|
-
cache_creation_input_tokens: null,
|
|
135
|
-
cache_read_input_tokens: usage?.input_tokens_details?.cached_tokens ?? null,
|
|
136
|
-
cache_creation: null,
|
|
137
|
-
inference_geo: null,
|
|
138
|
-
server_tool_use: webSearchCount > 0
|
|
139
|
-
? { web_search_requests: webSearchCount, web_fetch_requests: 0 }
|
|
140
|
-
: null,
|
|
141
|
-
service_tier: null,
|
|
142
|
-
},
|
|
131
|
+
usage,
|
|
143
132
|
container: null,
|
|
144
133
|
};
|
|
145
134
|
}
|
|
135
|
+
buildUsage(usage, webSearchCount) {
|
|
136
|
+
const u = usage ?? {};
|
|
137
|
+
const inputTokens = u.input_tokens ?? 0;
|
|
138
|
+
const outputTokens = u.output_tokens ?? 0;
|
|
139
|
+
const totalTokens = u.total_tokens ?? inputTokens + outputTokens;
|
|
140
|
+
const cachedTokens = u.input_tokens_details?.cached_tokens ?? 0;
|
|
141
|
+
const reasoningTokens = u.output_tokens_details?.reasoning_tokens ?? 0;
|
|
142
|
+
return {
|
|
143
|
+
completion_tokens: outputTokens,
|
|
144
|
+
prompt_tokens: inputTokens,
|
|
145
|
+
total_tokens: totalTokens,
|
|
146
|
+
completion_tokens_details: {
|
|
147
|
+
reasoning_tokens: reasoningTokens,
|
|
148
|
+
},
|
|
149
|
+
prompt_tokens_details: {
|
|
150
|
+
cached_tokens: cachedTokens,
|
|
151
|
+
...(webSearchCount > 0 ? { web_search: webSearchCount } : {}),
|
|
152
|
+
},
|
|
153
|
+
input_tokens: inputTokens,
|
|
154
|
+
output_tokens: outputTokens,
|
|
155
|
+
cache_read_input_tokens: cachedTokens,
|
|
156
|
+
server_tool_use: webSearchCount > 0
|
|
157
|
+
? {
|
|
158
|
+
web_fetch_requests: 0,
|
|
159
|
+
web_search_requests: webSearchCount,
|
|
160
|
+
}
|
|
161
|
+
: null,
|
|
162
|
+
cache_creation_input_tokens: 0,
|
|
163
|
+
service_tier: "standard",
|
|
164
|
+
audio_input_tokens: 0,
|
|
165
|
+
audio_cache_read_tokens: 0,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
146
168
|
// --- Stream conversion (Responses → Messages, backward) ---
|
|
147
169
|
async *convertStream(stream) {
|
|
148
170
|
for await (const event of stream) {
|
|
@@ -265,20 +287,11 @@ class MessagesToResponsesConverter {
|
|
|
265
287
|
case "response.completed": {
|
|
266
288
|
const completedEvent = event;
|
|
267
289
|
const resp = completedEvent.response;
|
|
268
|
-
const usage = resp.usage;
|
|
269
290
|
const stopReason = this.statusToStopReason(resp.status, resp.output);
|
|
270
291
|
events.push({
|
|
271
292
|
type: "message_delta",
|
|
272
293
|
delta: { stop_reason: stopReason, stop_sequence: null, container: null },
|
|
273
|
-
usage:
|
|
274
|
-
output_tokens: usage?.output_tokens ?? 0,
|
|
275
|
-
input_tokens: usage?.input_tokens ?? null,
|
|
276
|
-
cache_creation_input_tokens: null,
|
|
277
|
-
cache_read_input_tokens: usage?.input_tokens_details?.cached_tokens ?? null,
|
|
278
|
-
server_tool_use: state.webSearchCount > 0
|
|
279
|
-
? { web_search_requests: state.webSearchCount }
|
|
280
|
-
: null,
|
|
281
|
-
},
|
|
294
|
+
usage: this.buildUsage(resp.usage, state.webSearchCount),
|
|
282
295
|
});
|
|
283
296
|
events.push({ type: "message_stop" });
|
|
284
297
|
break;
|
|
@@ -286,17 +299,10 @@ class MessagesToResponsesConverter {
|
|
|
286
299
|
case "response.incomplete": {
|
|
287
300
|
const incompleteEvent = event;
|
|
288
301
|
const resp = incompleteEvent.response;
|
|
289
|
-
const usage = resp.usage;
|
|
290
302
|
events.push({
|
|
291
303
|
type: "message_delta",
|
|
292
304
|
delta: { stop_reason: "max_tokens", stop_sequence: null, container: null },
|
|
293
|
-
usage:
|
|
294
|
-
output_tokens: usage?.output_tokens ?? 0,
|
|
295
|
-
input_tokens: usage?.input_tokens ?? null,
|
|
296
|
-
cache_creation_input_tokens: null,
|
|
297
|
-
cache_read_input_tokens: null,
|
|
298
|
-
server_tool_use: null,
|
|
299
|
-
},
|
|
305
|
+
usage: this.buildUsage(resp.usage, state.webSearchCount),
|
|
300
306
|
});
|
|
301
307
|
events.push({ type: "message_stop" });
|
|
302
308
|
break;
|
|
@@ -305,13 +311,7 @@ class MessagesToResponsesConverter {
|
|
|
305
311
|
events.push({
|
|
306
312
|
type: "message_delta",
|
|
307
313
|
delta: { stop_reason: "end_turn", stop_sequence: null, container: null },
|
|
308
|
-
usage:
|
|
309
|
-
output_tokens: 0,
|
|
310
|
-
input_tokens: null,
|
|
311
|
-
cache_creation_input_tokens: null,
|
|
312
|
-
cache_read_input_tokens: null,
|
|
313
|
-
server_tool_use: null,
|
|
314
|
-
},
|
|
314
|
+
usage: this.buildUsage(null, state.webSearchCount),
|
|
315
315
|
});
|
|
316
316
|
events.push({ type: "message_stop" });
|
|
317
317
|
break;
|