@zenmux/rosetta-ai 1.0.27 → 1.0.29
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/dist/chat-completions/gemini.js +23 -8
- package/dist/chat-completions/messages.js +3 -2
- package/dist/gemini/messages.js +3 -2
- package/dist/responses/chat-completions.js +12 -8
- package/dist/responses/messages.js +2 -1
- package/dist/sanitize-anthropic-schema.d.ts +16 -0
- package/dist/sanitize-anthropic-schema.js +87 -0
- package/package.json +1 -1
|
@@ -17,10 +17,13 @@ class ChatCompletionToGeminiConverter {
|
|
|
17
17
|
systemParts.push({ text });
|
|
18
18
|
}
|
|
19
19
|
else if (msg.role === "user") {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
const parts = this.convertUserParts(msg.content);
|
|
21
|
+
if (parts.length > 0) {
|
|
22
|
+
contents.push({
|
|
23
|
+
role: "user",
|
|
24
|
+
parts,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
24
27
|
}
|
|
25
28
|
else if (msg.role === "assistant") {
|
|
26
29
|
for (const toolCall of msg.tool_calls ?? []) {
|
|
@@ -28,10 +31,13 @@ class ChatCompletionToGeminiConverter {
|
|
|
28
31
|
toolCallNames.set(toolCall.id, toolCall.function.name);
|
|
29
32
|
}
|
|
30
33
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
const parts = this.convertAssistantParts(msg);
|
|
35
|
+
if (parts.length > 0) {
|
|
36
|
+
contents.push({
|
|
37
|
+
role: "model",
|
|
38
|
+
parts,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
35
41
|
}
|
|
36
42
|
else if (msg.role === "tool") {
|
|
37
43
|
this.appendToolResponse(contents, msg, toolCallNames.get(msg.tool_call_id));
|
|
@@ -323,6 +329,15 @@ class ChatCompletionToGeminiConverter {
|
|
|
323
329
|
}
|
|
324
330
|
}
|
|
325
331
|
}
|
|
332
|
+
// Responses reasoning history is represented as the non-standard
|
|
333
|
+
// reasoning_content field in Chat Completions. The legacy conversion sent
|
|
334
|
+
// standalone reasoning summaries to Gemini as ordinary model text.
|
|
335
|
+
if (parts.length === 0) {
|
|
336
|
+
const reasoningContent = msg.reasoning_content;
|
|
337
|
+
if (typeof reasoningContent === "string" && reasoningContent.length > 0) {
|
|
338
|
+
parts.push({ text: reasoningContent });
|
|
339
|
+
}
|
|
340
|
+
}
|
|
326
341
|
return parts;
|
|
327
342
|
}
|
|
328
343
|
appendToolResponse(contents, msg, functionName) {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ChatCompletionToMessagesConverter = void 0;
|
|
4
|
+
const sanitize_anthropic_schema_1 = require("../sanitize-anthropic-schema");
|
|
4
5
|
const DEFAULT_MAX_TOKENS = 4096;
|
|
5
6
|
class ChatCompletionToMessagesConverter {
|
|
6
7
|
streamState;
|
|
@@ -524,9 +525,9 @@ class ChatCompletionToMessagesConverter {
|
|
|
524
525
|
type: "custom",
|
|
525
526
|
name: tool.function.name,
|
|
526
527
|
description: tool.function.description,
|
|
527
|
-
input_schema: (tool.function.parameters ?? {
|
|
528
|
+
input_schema: (0, sanitize_anthropic_schema_1.sanitizeAnthropicInputSchema)((tool.function.parameters ?? {
|
|
528
529
|
type: "object",
|
|
529
|
-
}),
|
|
530
|
+
})),
|
|
530
531
|
};
|
|
531
532
|
}
|
|
532
533
|
this.customToolNames.add(tool.custom.name);
|
package/dist/gemini/messages.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.GeminiToMessagesConverter = void 0;
|
|
4
|
+
const sanitize_anthropic_schema_1 = require("../sanitize-anthropic-schema");
|
|
4
5
|
class GeminiToMessagesConverter {
|
|
5
6
|
streamState;
|
|
6
7
|
constructor() {
|
|
@@ -277,8 +278,8 @@ class GeminiToMessagesConverter {
|
|
|
277
278
|
result.push({
|
|
278
279
|
name: fd.name ?? "",
|
|
279
280
|
description: fd.description,
|
|
280
|
-
input_schema: (fd.parametersJsonSchema ??
|
|
281
|
-
fd.parameters ?? { type: "object" }),
|
|
281
|
+
input_schema: (0, sanitize_anthropic_schema_1.sanitizeAnthropicInputSchema)((fd.parametersJsonSchema ??
|
|
282
|
+
fd.parameters ?? { type: "object" })),
|
|
282
283
|
});
|
|
283
284
|
}
|
|
284
285
|
}
|
|
@@ -842,18 +842,22 @@ class ResponsesToChatCompletionConverter {
|
|
|
842
842
|
}
|
|
843
843
|
else if (typed.role === "assistant") {
|
|
844
844
|
const textParts = [];
|
|
845
|
-
if (
|
|
845
|
+
if (typeof typed.content === "string") {
|
|
846
|
+
textParts.push(typed.content);
|
|
847
|
+
}
|
|
848
|
+
else if (Array.isArray(typed.content)) {
|
|
846
849
|
for (const part of typed.content) {
|
|
847
|
-
if (part.type === "output_text")
|
|
850
|
+
if (part.type === "output_text" || part.type === "input_text") {
|
|
848
851
|
textParts.push(part.text);
|
|
852
|
+
}
|
|
849
853
|
}
|
|
850
854
|
}
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
855
|
+
if (textParts.length > 0) {
|
|
856
|
+
messages.push({
|
|
857
|
+
role: "assistant",
|
|
858
|
+
content: textParts.join(""),
|
|
859
|
+
});
|
|
860
|
+
}
|
|
857
861
|
}
|
|
858
862
|
}
|
|
859
863
|
else if (typed.type === "reasoning") {
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ResponsesToMessagesConverter = void 0;
|
|
4
4
|
const sdk_1 = require("@anthropic-ai/sdk");
|
|
5
5
|
const utils_1 = require("./utils");
|
|
6
|
+
const sanitize_anthropic_schema_1 = require("../sanitize-anthropic-schema");
|
|
6
7
|
const DEFAULT_MAX_TOKENS = 4096;
|
|
7
8
|
class ResponsesToMessagesConverter {
|
|
8
9
|
streamState;
|
|
@@ -906,7 +907,7 @@ class ResponsesToMessagesConverter {
|
|
|
906
907
|
result.push({
|
|
907
908
|
name: tt.name,
|
|
908
909
|
description: tt.description,
|
|
909
|
-
input_schema: (tt.parameters ?? { type: "object" }),
|
|
910
|
+
input_schema: (0, sanitize_anthropic_schema_1.sanitizeAnthropicInputSchema)((tt.parameters ?? { type: "object" })),
|
|
910
911
|
});
|
|
911
912
|
}
|
|
912
913
|
else if (this.isWebSearch(tt)) {
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type Anthropic from "@anthropic-ai/sdk";
|
|
2
|
+
/**
|
|
3
|
+
* Anthropic's Messages API rejects `oneOf` / `anyOf` / `allOf` at the *top level*
|
|
4
|
+
* of a tool `input_schema` ("input_schema does not support oneOf, allOf, or anyOf
|
|
5
|
+
* at the top level"). Some clients describe a tool as a discriminated union with a
|
|
6
|
+
* top-level combinator — e.g. Codex namespace tools such as
|
|
7
|
+
* `mcp__codex_app/automation_update`, whose schema is
|
|
8
|
+
* `{ type: "object", properties: {}, oneOf: [<branch>, ...], $defs: {...} }`.
|
|
9
|
+
*
|
|
10
|
+
* Flatten the top-level combinator into a plain object schema by unioning the
|
|
11
|
+
* branch properties, so every parameter stays visible to the model. Nested
|
|
12
|
+
* combinators (inside properties / `$defs`) are left untouched — Anthropic allows
|
|
13
|
+
* those. This relaxes the branch-exclusivity hint but preserves the parameter
|
|
14
|
+
* set; the tool's own server still validates the concrete call.
|
|
15
|
+
*/
|
|
16
|
+
export declare function sanitizeAnthropicInputSchema(schema: Anthropic.Tool.InputSchema): Anthropic.Tool.InputSchema;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sanitizeAnthropicInputSchema = sanitizeAnthropicInputSchema;
|
|
4
|
+
const TOP_LEVEL_COMBINATORS = ["oneOf", "anyOf", "allOf"];
|
|
5
|
+
/**
|
|
6
|
+
* Anthropic's Messages API rejects `oneOf` / `anyOf` / `allOf` at the *top level*
|
|
7
|
+
* of a tool `input_schema` ("input_schema does not support oneOf, allOf, or anyOf
|
|
8
|
+
* at the top level"). Some clients describe a tool as a discriminated union with a
|
|
9
|
+
* top-level combinator — e.g. Codex namespace tools such as
|
|
10
|
+
* `mcp__codex_app/automation_update`, whose schema is
|
|
11
|
+
* `{ type: "object", properties: {}, oneOf: [<branch>, ...], $defs: {...} }`.
|
|
12
|
+
*
|
|
13
|
+
* Flatten the top-level combinator into a plain object schema by unioning the
|
|
14
|
+
* branch properties, so every parameter stays visible to the model. Nested
|
|
15
|
+
* combinators (inside properties / `$defs`) are left untouched — Anthropic allows
|
|
16
|
+
* those. This relaxes the branch-exclusivity hint but preserves the parameter
|
|
17
|
+
* set; the tool's own server still validates the concrete call.
|
|
18
|
+
*/
|
|
19
|
+
function sanitizeAnthropicInputSchema(schema) {
|
|
20
|
+
if (!schema || typeof schema !== "object")
|
|
21
|
+
return schema;
|
|
22
|
+
const s = schema;
|
|
23
|
+
const combinator = TOP_LEVEL_COMBINATORS.find(k => Array.isArray(s[k]));
|
|
24
|
+
if (!combinator)
|
|
25
|
+
return schema;
|
|
26
|
+
const defs = s.$defs ?? s.definitions;
|
|
27
|
+
const seen = new Set();
|
|
28
|
+
const branches = s[combinator].flatMap(b => collectObjectSchemas(b, defs, seen));
|
|
29
|
+
const properties = { ...(s.properties ?? {}) };
|
|
30
|
+
const requiredSets = [];
|
|
31
|
+
for (const branch of branches) {
|
|
32
|
+
if (branch.properties)
|
|
33
|
+
Object.assign(properties, branch.properties);
|
|
34
|
+
requiredSets.push(Array.isArray(branch.required) ? branch.required : []);
|
|
35
|
+
}
|
|
36
|
+
// required = properties required in every branch (safe intersection); a union
|
|
37
|
+
// of branches can't require branch-specific fields.
|
|
38
|
+
const required = requiredSets.length > 0
|
|
39
|
+
? requiredSets.reduce((acc, cur) => acc.filter(name => cur.includes(name)))
|
|
40
|
+
: [];
|
|
41
|
+
const result = { ...s, type: "object", properties };
|
|
42
|
+
for (const k of TOP_LEVEL_COMBINATORS)
|
|
43
|
+
delete result[k];
|
|
44
|
+
if (required.length > 0)
|
|
45
|
+
result.required = required;
|
|
46
|
+
else
|
|
47
|
+
delete result.required;
|
|
48
|
+
// The union spans previously-disjoint branches, so don't forbid other branches' props.
|
|
49
|
+
delete result.additionalProperties;
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Walk a combinator branch to its object-schema leaves, following `$ref` and
|
|
54
|
+
* recursing through nested `oneOf` / `anyOf` / `allOf` (Codex nests union modes
|
|
55
|
+
* two levels deep: top-level oneOf → per-mode oneOf → object). Deduped by `$ref`
|
|
56
|
+
* so shared defs and cycles are visited once.
|
|
57
|
+
*/
|
|
58
|
+
function collectObjectSchemas(node, defs, seen) {
|
|
59
|
+
if (node && typeof node === "object" && typeof node.$ref === "string") {
|
|
60
|
+
if (seen.has(node.$ref))
|
|
61
|
+
return [];
|
|
62
|
+
seen.add(node.$ref);
|
|
63
|
+
}
|
|
64
|
+
const resolved = resolveRef(node, defs);
|
|
65
|
+
if (!resolved || typeof resolved !== "object")
|
|
66
|
+
return [];
|
|
67
|
+
const combinator = TOP_LEVEL_COMBINATORS.find(k => Array.isArray(resolved[k]));
|
|
68
|
+
if (combinator) {
|
|
69
|
+
return resolved[combinator].flatMap(b => collectObjectSchemas(b, defs, seen));
|
|
70
|
+
}
|
|
71
|
+
if (resolved.type === "object" || resolved.properties)
|
|
72
|
+
return [resolved];
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
// ponytail: one level of $ref resolution against $defs/definitions; deep/remote refs unsupported (Codex schemas ref one hop).
|
|
76
|
+
function resolveRef(node, defs) {
|
|
77
|
+
let cur = node;
|
|
78
|
+
const seen = new Set();
|
|
79
|
+
while (cur && typeof cur === "object" && typeof cur.$ref === "string") {
|
|
80
|
+
const m = /^#\/(?:\$defs|definitions)\/(.+)$/.exec(cur.$ref);
|
|
81
|
+
if (!m || !defs || seen.has(cur.$ref))
|
|
82
|
+
break;
|
|
83
|
+
seen.add(cur.$ref);
|
|
84
|
+
cur = defs[m[1]];
|
|
85
|
+
}
|
|
86
|
+
return cur;
|
|
87
|
+
}
|