@tanstack/ai-mistral 0.3.4 → 0.4.0

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.
Files changed (42) hide show
  1. package/dist/esm/adapters/text.d.ts +2 -2
  2. package/dist/esm/adapters/text.js +16 -12
  3. package/dist/esm/adapters/text.js.map +1 -1
  4. package/dist/esm/index.d.ts +2 -2
  5. package/dist/esm/index.js +2 -2
  6. package/dist/esm/model-meta.d.ts +71 -0
  7. package/dist/esm/model-meta.js +335 -242
  8. package/dist/esm/model-meta.js.map +1 -1
  9. package/dist/esm/tools/function-tool.d.ts +4 -5
  10. package/dist/esm/tools/function-tool.js +7 -8
  11. package/dist/esm/tools/function-tool.js.map +1 -1
  12. package/dist/esm/tools/function-tool.test.d.ts +1 -0
  13. package/dist/esm/utils/client.d.ts +12 -0
  14. package/dist/esm/utils/client.js +9 -5
  15. package/dist/esm/utils/client.js.map +1 -1
  16. package/dist/esm/utils/schema-converter.d.ts +15 -15
  17. package/dist/esm/utils/schema-converter.js +173 -50
  18. package/dist/esm/utils/schema-converter.js.map +1 -1
  19. package/dist/esm/utils/schema-converter.test.d.ts +1 -0
  20. package/dist/esm/utils/tool-input-normalizer.d.ts +8 -0
  21. package/dist/esm/utils/tool-input-normalizer.js +34 -0
  22. package/dist/esm/utils/tool-input-normalizer.js.map +1 -0
  23. package/dist/esm/utils/tool-input-normalizer.test.d.ts +1 -0
  24. package/dist/esm/vertex/auth.d.ts +29 -0
  25. package/dist/esm/vertex/auth.js +62 -0
  26. package/dist/esm/vertex/auth.js.map +1 -0
  27. package/dist/esm/vertex/index.d.ts +12 -0
  28. package/dist/esm/vertex/index.js +26 -0
  29. package/dist/esm/vertex/index.js.map +1 -0
  30. package/package.json +16 -4
  31. package/src/adapters/text.ts +40 -20
  32. package/src/index.ts +6 -1
  33. package/src/model-meta.ts +70 -0
  34. package/src/tools/function-tool.test.ts +32 -0
  35. package/src/tools/function-tool.ts +11 -11
  36. package/src/utils/client.ts +41 -6
  37. package/src/utils/schema-converter.test.ts +256 -0
  38. package/src/utils/schema-converter.ts +251 -88
  39. package/src/utils/tool-input-normalizer.test.ts +50 -0
  40. package/src/utils/tool-input-normalizer.ts +42 -0
  41. package/src/vertex/auth.ts +154 -0
  42. package/src/vertex/index.ts +50 -0
@@ -0,0 +1,256 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { makeMistralStructuredOutputCompatibleWithMap } from './schema-converter'
3
+
4
+ describe('makeMistralStructuredOutputCompatibleWithMap', () => {
5
+ it('widens optional enum and const constraints to admit the omission marker', () => {
6
+ const { schema, nullWideningMap } =
7
+ makeMistralStructuredOutputCompatibleWithMap(
8
+ {
9
+ type: 'object',
10
+ properties: {
11
+ enumValue: { type: 'string', enum: ['canary'] },
12
+ constValue: { type: 'string', const: 'fixed' },
13
+ },
14
+ required: [],
15
+ },
16
+ [],
17
+ )
18
+
19
+ expect(schema).toEqual({
20
+ type: 'object',
21
+ properties: {
22
+ enumValue: {
23
+ type: ['string', 'null'],
24
+ enum: ['canary', null],
25
+ },
26
+ constValue: {
27
+ type: ['string', 'null'],
28
+ enum: ['fixed', null],
29
+ },
30
+ },
31
+ required: ['enumValue', 'constValue'],
32
+ additionalProperties: false,
33
+ })
34
+ expect(nullWideningMap).toEqual({
35
+ properties: {
36
+ enumValue: { widened: true },
37
+ constValue: { widened: true },
38
+ },
39
+ })
40
+ })
41
+
42
+ it('does not mark original nullable fields as synthesized', () => {
43
+ const { nullWideningMap } = makeMistralStructuredOutputCompatibleWithMap(
44
+ {
45
+ type: 'object',
46
+ properties: {
47
+ value: { type: ['string', 'null'] },
48
+ },
49
+ required: [],
50
+ },
51
+ [],
52
+ )
53
+
54
+ expect(nullWideningMap).toBeUndefined()
55
+ })
56
+
57
+ it('does not rewrap a node that already accepts null', () => {
58
+ const note = { anyOf: [{ type: 'string' }, { type: 'null' }] }
59
+ const { schema, nullWideningMap } =
60
+ makeMistralStructuredOutputCompatibleWithMap(
61
+ {
62
+ type: 'object',
63
+ properties: { note },
64
+ required: [],
65
+ },
66
+ [],
67
+ )
68
+
69
+ expect(schema.properties).toEqual({ note })
70
+ expect(nullWideningMap).toBeUndefined()
71
+ })
72
+
73
+ it('adds null to enum and const when type already includes null', () => {
74
+ const { schema, nullWideningMap } =
75
+ makeMistralStructuredOutputCompatibleWithMap(
76
+ {
77
+ type: 'object',
78
+ properties: {
79
+ enumValue: { type: ['string', 'null'], enum: ['canary'] },
80
+ constValue: { type: ['string', 'null'], const: 'fixed' },
81
+ note: { type: ['string', 'null'] },
82
+ },
83
+ required: ['enumValue', 'constValue', 'note'],
84
+ },
85
+ ['enumValue', 'constValue', 'note'],
86
+ )
87
+
88
+ expect(schema.properties).toEqual({
89
+ enumValue: {
90
+ type: ['string', 'null'],
91
+ enum: ['canary', null],
92
+ },
93
+ constValue: {
94
+ type: ['string', 'null'],
95
+ enum: ['fixed', null],
96
+ },
97
+ note: { type: ['string', 'null'] },
98
+ })
99
+ expect(nullWideningMap).toBeUndefined()
100
+ })
101
+
102
+ it('recurses into objects whose type is a union that includes object', () => {
103
+ const { schema, nullWideningMap } =
104
+ makeMistralStructuredOutputCompatibleWithMap(
105
+ {
106
+ type: 'object',
107
+ properties: {
108
+ nested: {
109
+ type: ['object', 'null'],
110
+ properties: {
111
+ mode: { type: ['string', 'null'], enum: ['canary'] },
112
+ },
113
+ required: ['mode'],
114
+ additionalProperties: false,
115
+ },
116
+ },
117
+ required: ['nested'],
118
+ },
119
+ ['nested'],
120
+ )
121
+
122
+ expect(schema.properties).toEqual({
123
+ nested: {
124
+ type: ['object', 'null'],
125
+ properties: {
126
+ mode: {
127
+ type: ['string', 'null'],
128
+ enum: ['canary', null],
129
+ },
130
+ },
131
+ required: ['mode'],
132
+ additionalProperties: false,
133
+ },
134
+ })
135
+ expect(nullWideningMap).toBeUndefined()
136
+ })
137
+
138
+ it('preserves boolean schemas while widening optional properties', () => {
139
+ const inputSchema = {
140
+ type: 'object',
141
+ properties: {},
142
+ required: [],
143
+ }
144
+ Reflect.set(inputSchema.properties, 'acceptAnything', true)
145
+ Reflect.set(inputSchema.properties, 'rejectAnything', false)
146
+
147
+ const { schema, nullWideningMap } =
148
+ makeMistralStructuredOutputCompatibleWithMap(inputSchema, [])
149
+
150
+ expect(schema).toEqual({
151
+ type: 'object',
152
+ properties: {
153
+ acceptAnything: true,
154
+ rejectAnything: { anyOf: [false, { type: 'null' }] },
155
+ },
156
+ required: ['acceptAnything', 'rejectAnything'],
157
+ additionalProperties: false,
158
+ })
159
+ expect(nullWideningMap).toEqual({
160
+ properties: {
161
+ rejectAnything: { widened: true },
162
+ },
163
+ })
164
+ })
165
+
166
+ it('keeps tuple items as an array and widens optional tuple slots', () => {
167
+ const { schema, nullWideningMap, strict } =
168
+ makeMistralStructuredOutputCompatibleWithMap(
169
+ {
170
+ type: 'object',
171
+ properties: {
172
+ pair: {
173
+ type: 'array',
174
+ items: [
175
+ {
176
+ type: 'object',
177
+ properties: { label: { type: 'string' } },
178
+ required: [],
179
+ },
180
+ { type: 'string' },
181
+ ],
182
+ },
183
+ },
184
+ required: [],
185
+ },
186
+ [],
187
+ )
188
+
189
+ expect(strict).toBe(true)
190
+ expect(Array.isArray(schema.properties.pair.items)).toBe(true)
191
+ expect(schema.properties.pair).toEqual({
192
+ type: ['array', 'null'],
193
+ items: [
194
+ {
195
+ type: 'object',
196
+ properties: { label: { type: ['string', 'null'] } },
197
+ required: ['label'],
198
+ additionalProperties: false,
199
+ },
200
+ { type: 'string' },
201
+ ],
202
+ })
203
+ expect(nullWideningMap).toEqual({
204
+ properties: {
205
+ pair: {
206
+ widened: true,
207
+ items: [{ properties: { label: { widened: true } } }, {}],
208
+ },
209
+ },
210
+ })
211
+ })
212
+
213
+ it('falls back from strict mode for branch-dependent compositions', () => {
214
+ const input = {
215
+ type: 'object',
216
+ properties: {
217
+ allOfValue: { allOf: [{ type: 'string' }] },
218
+ oneOfValue: {
219
+ oneOf: [{ type: 'string' }, { type: 'number' }],
220
+ },
221
+ notNullValue: { not: { type: 'null' } },
222
+ },
223
+ required: [],
224
+ }
225
+ const result = makeMistralStructuredOutputCompatibleWithMap(input, [])
226
+
227
+ expect(result).toEqual({
228
+ schema: input,
229
+ nullWideningMap: undefined,
230
+ strict: false,
231
+ })
232
+ })
233
+
234
+ it('falls back when an anyOf branch needs branch-dependent widening', () => {
235
+ const input = {
236
+ anyOf: [
237
+ {
238
+ type: 'object',
239
+ properties: { optional: { type: 'string' } },
240
+ required: [],
241
+ },
242
+ {
243
+ type: 'object',
244
+ properties: { optional: { type: ['string', 'null'] } },
245
+ required: ['optional'],
246
+ },
247
+ ],
248
+ }
249
+
250
+ expect(makeMistralStructuredOutputCompatibleWithMap(input, [])).toEqual({
251
+ schema: input,
252
+ nullWideningMap: undefined,
253
+ strict: false,
254
+ })
255
+ })
256
+ })
@@ -1,117 +1,252 @@
1
+ import type { NullWideningMap } from '@tanstack/ai-utils'
2
+
3
+ interface MistralStructuredOutputCompatibility {
4
+ schema: Record<string, any>
5
+ nullWideningMap: NullWideningMap | undefined
6
+ strict: boolean
7
+ }
8
+
1
9
  /**
2
- * Recursively transform null values to undefined in an object.
10
+ * Convert a schema for Mistral strict mode and record how to invert it.
3
11
  *
4
- * This is needed because Mistral's structured output may require optional
5
- * fields to be declared nullable. When Mistral returns null for optional
6
- * fields, we convert them back to undefined to match the original Zod schema.
12
+ * Outcomes:
13
+ * - `strict: true` — rewritten schema (`required` closed, optionals null-widened).
14
+ * `nullWideningMap` marks synthesized optional nulls only; already-nullable
15
+ * fields and enum/const repairs on required nodes are unmarked.
16
+ * - `strict: false` — original schema, no map. Used when `oneOf`/`allOf`/`not`/
17
+ * `$ref`/`$defs` appear, or an `anyOf` branch would need a branch-dependent map.
7
18
  */
8
- export function transformNullsToUndefined<T>(obj: T): T {
9
- if (obj === null) {
10
- // oxlint-disable-next-line eslint-js/no-restricted-syntax -- generic T has no structural overlap with undefined; null→undefined conversion is this function's documented contract
11
- return undefined as unknown as T
19
+ export function makeMistralStructuredOutputCompatibleWithMap(
20
+ schema: Record<string, any>,
21
+ originalRequired: Array<string> = [],
22
+ ): MistralStructuredOutputCompatibility {
23
+ if (containsUnsupportedStrictKeyword(schema)) {
24
+ return { schema, nullWideningMap: undefined, strict: false }
12
25
  }
13
26
 
14
- if (Array.isArray(obj)) {
15
- // Preserve array length and indices — converting null elements to
16
- // undefined slots rather than dropping them. `Array<T | null>` schemas
17
- // depend on positional alignment.
18
- // oxlint-disable-next-line eslint-js/no-restricted-syntax -- the mapped array (unknown[]) has no structural overlap with generic T; positional null→undefined transform preserves the array shape T describes
19
- return obj.map((item) => transformNullsToUndefined(item)) as unknown as T
27
+ const converted = coerceMistralStrictSchema(schema, originalRequired)
28
+ if (converted.hasUntrackableAnyOfWidening) {
29
+ return { schema, nullWideningMap: undefined, strict: false }
20
30
  }
21
31
 
22
- if (
23
- typeof obj === 'object' &&
24
- Object.getPrototypeOf(obj) === Object.prototype
25
- ) {
26
- // Preserve every key — `null` values become `undefined` values, but the
27
- // key itself is not removed. Schemas distinguishing absent vs explicit
28
- // null rely on this.
29
- const result: Record<string, unknown> = {}
30
- for (const [key, value] of Object.entries(obj as Record<string, unknown>)) {
31
- result[key] = transformNullsToUndefined(value)
32
- }
33
- return result as T
32
+ return {
33
+ schema: converted.schema,
34
+ nullWideningMap: converted.nullWideningMap,
35
+ strict: true,
34
36
  }
37
+ }
35
38
 
36
- return obj
39
+ interface CoercedMistralStrictSchema {
40
+ schema: Record<string, any>
41
+ nullWideningMap: NullWideningMap | undefined
42
+ hasUntrackableAnyOfWidening: boolean
37
43
  }
38
44
 
45
+ const UNSUPPORTED_STRICT_KEYWORDS: ReadonlyArray<string> = [
46
+ 'oneOf',
47
+ 'allOf',
48
+ 'not',
49
+ '$ref',
50
+ '$defs',
51
+ 'definitions',
52
+ ]
53
+
39
54
  /**
40
- * Transform a JSON schema to be compatible with Mistral's structured output
41
- * requirements when `strict: true` is used.
42
- *
43
- * Mistral (in strict mode) requires:
44
- * - All properties must be in the `required` array
45
- * - Optional fields should have null added to their type union
46
- * - additionalProperties must be false for objects
55
+ * Tree-wide key scan for `oneOf`/`allOf`/`not`/`$ref`/`$defs`/`definitions`.
56
+ * Conservative: a property literally named e.g. `oneOf` also trips fallback.
57
+ * `anyOf` is handled separately in the coerce walk.
47
58
  */
48
- export function makeMistralStructuredOutputCompatible(
59
+ function containsUnsupportedStrictKeyword(node: unknown): boolean {
60
+ if (Array.isArray(node)) return node.some(containsUnsupportedStrictKeyword)
61
+ if (!isSchemaObject(node)) return false
62
+
63
+ return Object.entries(node).some(
64
+ ([key, value]) =>
65
+ UNSUPPORTED_STRICT_KEYWORDS.includes(key) ||
66
+ containsUnsupportedStrictKeyword(value),
67
+ )
68
+ }
69
+
70
+ function pruneMap(map: NullWideningMap): NullWideningMap | undefined {
71
+ return Object.keys(map).length > 0 ? map : undefined
72
+ }
73
+
74
+ function isSchemaObject(schema: unknown): schema is Record<string, any> {
75
+ return typeof schema === 'object' && schema !== null && !Array.isArray(schema)
76
+ }
77
+
78
+ function coerceArrayItems(items: unknown): {
79
+ schema: unknown
80
+ itemMap: NullWideningMap | Array<NullWideningMap> | undefined
81
+ hasUntrackableAnyOfWidening: boolean
82
+ } {
83
+ if (Array.isArray(items)) {
84
+ const converted = items.map((item) =>
85
+ isSchemaObject(item)
86
+ ? coerceMistralStrictSchema(item, item.required || [])
87
+ : {
88
+ schema: item,
89
+ nullWideningMap: undefined,
90
+ hasUntrackableAnyOfWidening: false,
91
+ },
92
+ )
93
+ const itemMaps = converted.map((item) => item.nullWideningMap)
94
+ return {
95
+ schema: converted.map((item) => item.schema),
96
+ itemMap: itemMaps.some(Boolean)
97
+ ? itemMaps.map((map) => map ?? {})
98
+ : undefined,
99
+ hasUntrackableAnyOfWidening: converted.some(
100
+ (item) => item.hasUntrackableAnyOfWidening,
101
+ ),
102
+ }
103
+ }
104
+
105
+ if (isSchemaObject(items)) {
106
+ const converted = coerceMistralStrictSchema(items, items.required || [])
107
+ return {
108
+ schema: converted.schema,
109
+ itemMap: converted.nullWideningMap,
110
+ hasUntrackableAnyOfWidening: converted.hasUntrackableAnyOfWidening,
111
+ }
112
+ }
113
+
114
+ return {
115
+ schema: items,
116
+ itemMap: undefined,
117
+ hasUntrackableAnyOfWidening: false,
118
+ }
119
+ }
120
+
121
+ function schemaTypeIncludes(
49
122
  schema: Record<string, any>,
50
- originalRequired: Array<string> = [],
123
+ typeName: string,
124
+ ): boolean {
125
+ return (
126
+ schema.type === typeName ||
127
+ (Array.isArray(schema.type) && schema.type.includes(typeName))
128
+ )
129
+ }
130
+
131
+ function admitNullInEnumOrConst(
132
+ prop: Record<string, any>,
51
133
  ): Record<string, any> {
134
+ if ('const' in prop && prop.const !== null) {
135
+ const { const: constValue, ...withoutConst } = prop
136
+ return { ...withoutConst, enum: [constValue, null] }
137
+ }
138
+ if (Array.isArray(prop.enum) && !prop.enum.includes(null)) {
139
+ return { ...prop, enum: [...prop.enum, null] }
140
+ }
141
+ return prop
142
+ }
143
+
144
+ /**
145
+ * True when `type`/`enum`/`const`/`anyOf` already admit null. `oneOf`/`allOf`/
146
+ * `not` are not inspected — callers must reject those first.
147
+ */
148
+ function acceptsNull(schema: unknown): boolean {
149
+ if (schema === true) return true
150
+ if (!isSchemaObject(schema)) return false
151
+
152
+ if ('const' in schema && schema.const !== null) return false
153
+ if (Array.isArray(schema.enum) && !schema.enum.includes(null)) return false
154
+
155
+ if (typeof schema.type === 'string' && schema.type !== 'null') return false
156
+ if (Array.isArray(schema.type) && !schema.type.includes('null')) return false
157
+
158
+ if (
159
+ Array.isArray(schema.anyOf) &&
160
+ !schema.anyOf.some((variant: unknown) => acceptsNull(variant))
161
+ ) {
162
+ return false
163
+ }
164
+
165
+ return true
166
+ }
167
+
168
+ function coerceMistralStrictSchema(
169
+ schema: Record<string, any>,
170
+ originalRequired: Array<string>,
171
+ ): CoercedMistralStrictSchema {
52
172
  const result = { ...schema }
173
+ const nullWideningMap: NullWideningMap = {}
174
+ let hasUntrackableAnyOfWidening = false
53
175
 
54
- if (result.type === 'object') {
176
+ if (schemaTypeIncludes(result, 'object')) {
55
177
  if (!result.properties) {
56
178
  result.properties = {}
57
179
  }
58
180
  const properties = { ...result.properties }
59
181
  const allPropertyNames = Object.keys(properties)
182
+ const propertyMaps: Record<string, NullWideningMap> = {}
60
183
 
61
184
  for (const propName of allPropertyNames) {
62
- const prop = properties[propName]
185
+ let prop = properties[propName]
63
186
  const wasOptional = !originalRequired.includes(propName)
187
+ let childMap: NullWideningMap | undefined
188
+ let widenedHere = false
64
189
 
65
- if (prop.type === 'object' && prop.properties) {
66
- const converted = makeMistralStructuredOutputCompatible(
67
- prop,
68
- prop.required || [],
69
- )
70
- if (wasOptional) {
71
- properties[propName] = {
72
- ...converted,
73
- type: Array.isArray(converted.type)
74
- ? converted.type.includes('null')
75
- ? converted.type
76
- : [...converted.type, 'null']
77
- : [converted.type, 'null'],
78
- }
79
- } else {
80
- properties[propName] = converted
81
- }
82
- } else if (prop.type === 'array' && prop.items) {
83
- const converted = {
190
+ if (
191
+ isSchemaObject(prop) &&
192
+ schemaTypeIncludes(prop, 'object') &&
193
+ prop.properties
194
+ ) {
195
+ const converted = coerceMistralStrictSchema(prop, prop.required || [])
196
+ prop = converted.schema
197
+ childMap = converted.nullWideningMap
198
+ hasUntrackableAnyOfWidening ||= converted.hasUntrackableAnyOfWidening
199
+ } else if (
200
+ isSchemaObject(prop) &&
201
+ schemaTypeIncludes(prop, 'array') &&
202
+ prop.items != null
203
+ ) {
204
+ const convertedItems = coerceArrayItems(prop.items)
205
+ prop = {
84
206
  ...prop,
85
- items: makeMistralStructuredOutputCompatible(
86
- prop.items,
87
- prop.items.required || [],
88
- ),
207
+ items: convertedItems.schema,
89
208
  }
90
- if (wasOptional) {
91
- properties[propName] = {
92
- ...converted,
93
- type: Array.isArray(converted.type)
94
- ? converted.type.includes('null')
95
- ? converted.type
96
- : [...converted.type, 'null']
97
- : [converted.type, 'null'],
98
- }
99
- } else {
100
- properties[propName] = converted
209
+ if (convertedItems.itemMap) {
210
+ childMap = { items: convertedItems.itemMap }
101
211
  }
102
- } else if (wasOptional) {
103
- if (prop.type && !Array.isArray(prop.type)) {
104
- properties[propName] = {
105
- ...prop,
106
- type: [prop.type, 'null'],
212
+ hasUntrackableAnyOfWidening ||=
213
+ convertedItems.hasUntrackableAnyOfWidening
214
+ } else if (isSchemaObject(prop) && Array.isArray(prop.anyOf)) {
215
+ const converted = coerceMistralStrictSchema(prop, prop.required || [])
216
+ prop = converted.schema
217
+ childMap = converted.nullWideningMap
218
+ hasUntrackableAnyOfWidening ||= converted.hasUntrackableAnyOfWidening
219
+ }
220
+
221
+ if (!acceptsNull(prop)) {
222
+ if (wasOptional) {
223
+ if (isSchemaObject(prop)) {
224
+ prop = admitNullInEnumOrConst(prop)
107
225
  }
108
- } else if (Array.isArray(prop.type) && !prop.type.includes('null')) {
109
- properties[propName] = {
110
- ...prop,
111
- type: [...prop.type, 'null'],
226
+
227
+ if (isSchemaObject(prop) && prop.type && !Array.isArray(prop.type)) {
228
+ prop = { ...prop, type: [prop.type, 'null'] }
229
+ } else if (
230
+ isSchemaObject(prop) &&
231
+ Array.isArray(prop.type) &&
232
+ !prop.type.includes('null')
233
+ ) {
234
+ prop = { ...prop, type: [...prop.type, 'null'] }
235
+ } else if (!isSchemaObject(prop) || !prop.type) {
236
+ prop = { anyOf: [prop, { type: 'null' }] }
112
237
  }
113
- } else if (!prop.type) {
114
- properties[propName] = { anyOf: [prop, { type: 'null' }] }
238
+
239
+ widenedHere = true
240
+ } else if (isSchemaObject(prop) && schemaTypeIncludes(prop, 'null')) {
241
+ prop = admitNullInEnumOrConst(prop)
242
+ }
243
+ }
244
+
245
+ properties[propName] = prop
246
+ if (childMap || widenedHere) {
247
+ propertyMaps[propName] = {
248
+ ...(childMap ?? {}),
249
+ ...(widenedHere ? { widened: true } : {}),
115
250
  }
116
251
  }
117
252
  }
@@ -123,14 +258,42 @@ export function makeMistralStructuredOutputCompatible(
123
258
  delete result.required
124
259
  }
125
260
  result.additionalProperties = false
261
+ if (Object.keys(propertyMaps).length > 0) {
262
+ nullWideningMap.properties = propertyMaps
263
+ }
264
+ }
265
+
266
+ if (schemaTypeIncludes(result, 'array') && result.items != null) {
267
+ const convertedItems = coerceArrayItems(result.items)
268
+ result.items = convertedItems.schema
269
+ if (convertedItems.itemMap) {
270
+ nullWideningMap.items = convertedItems.itemMap
271
+ }
272
+ hasUntrackableAnyOfWidening ||= convertedItems.hasUntrackableAnyOfWidening
126
273
  }
127
274
 
128
- if (result.type === 'array' && result.items) {
129
- result.items = makeMistralStructuredOutputCompatible(
130
- result.items,
131
- result.items.required || [],
275
+ if (Array.isArray(result.anyOf)) {
276
+ const variants = result.anyOf.map((variant: unknown) => {
277
+ if (!isSchemaObject(variant)) {
278
+ return {
279
+ schema: variant,
280
+ nullWideningMap: undefined,
281
+ hasUntrackableAnyOfWidening: false,
282
+ }
283
+ }
284
+ return coerceMistralStrictSchema(variant, variant.required || [])
285
+ })
286
+ result.anyOf = variants.map((variant) => variant.schema)
287
+ hasUntrackableAnyOfWidening ||= variants.some(
288
+ (variant) =>
289
+ variant.nullWideningMap !== undefined ||
290
+ variant.hasUntrackableAnyOfWidening,
132
291
  )
133
292
  }
134
293
 
135
- return result
294
+ return {
295
+ schema: result,
296
+ nullWideningMap: pruneMap(nullWideningMap),
297
+ hasUntrackableAnyOfWidening,
298
+ }
136
299
  }
@@ -0,0 +1,50 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { createToolInputNormalizer } from './tool-input-normalizer'
3
+ import type { Tool } from '@tanstack/ai'
4
+
5
+ describe('createToolInputNormalizer', () => {
6
+ it('removes only nulls synthesized for optional fields', () => {
7
+ const tool: Tool = {
8
+ name: 'optional_enum',
9
+ description: 'Uses optional and genuinely nullable values',
10
+ inputSchema: {
11
+ type: 'object',
12
+ properties: {
13
+ mode: { type: 'string', enum: ['canary'] },
14
+ note: { type: ['string', 'null'] },
15
+ },
16
+ required: [],
17
+ },
18
+ }
19
+ const normalize = createToolInputNormalizer([tool])
20
+
21
+ expect(normalize(tool.name, { mode: null, note: null })).toEqual({
22
+ note: null,
23
+ })
24
+ })
25
+
26
+ it('does not guess when public tool names are ambiguous', () => {
27
+ const tools: Array<Tool> = [
28
+ {
29
+ name: 'duplicate',
30
+ description: 'First tool',
31
+ inputSchema: {
32
+ type: 'object',
33
+ properties: { firstOptional: { type: 'string' } },
34
+ },
35
+ },
36
+ {
37
+ name: 'duplicate',
38
+ description: 'Second tool',
39
+ inputSchema: {
40
+ type: 'object',
41
+ properties: { secondOptional: { type: 'string' } },
42
+ },
43
+ },
44
+ ]
45
+ const normalize = createToolInputNormalizer(tools)
46
+ const input = { firstOptional: null, secondOptional: null }
47
+
48
+ expect(normalize('duplicate', input)).toBe(input)
49
+ })
50
+ })