@zenmux/rosetta-ai 1.0.28 → 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.
@@ -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);
@@ -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
  }
@@ -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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenmux/rosetta-ai",
3
- "version": "1.0.28",
3
+ "version": "1.0.29",
4
4
  "description": "Universal translator between AI provider protocols",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",