@tanstack/ai-mistral 0.3.4 → 0.4.1

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 +4 -4
  2. package/dist/esm/adapters/text.js +18 -14
  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 +48 -28
  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,12 @@
1
+ import { MistralTextAdapter } from '../adapters/text.js';
2
+ import { MistralVertexChatModel } from '../model-meta.js';
3
+ import { MistralVertexConfig } from './auth.js';
4
+ export { MistralVertexAuthError, resolveMistralVertexAccessToken, resolveMistralVertexLocation, resolveMistralVertexModelUrl, resolveMistralVertexProject, type MistralVertexConfig, type VertexAuthClient, } from './auth.js';
5
+ export { MISTRAL_VERTEX_CHAT_MODELS, type MistralVertexChatModel, } from '../model-meta.js';
6
+ /**
7
+ * Creates a Mistral chat adapter that talks to Mistral on Vertex AI.
8
+ *
9
+ * Install `google-auth-library` next to `@tanstack/ai-mistral` for
10
+ * Application Default Credentials. Or pass `authClient` or `getAccessToken`.
11
+ */
12
+ export declare function mistralVertexText<TModel extends MistralVertexChatModel>(model: TModel, config?: MistralVertexConfig): MistralTextAdapter<TModel>;
@@ -0,0 +1,26 @@
1
+ import { MistralTextAdapter } from "../adapters/text.js";
2
+ import { MISTRAL_VERTEX_CHAT_MODELS } from "../model-meta.js";
3
+ import { MistralVertexAuthError, resolveMistralVertexAccessToken, resolveMistralVertexLocation, resolveMistralVertexModelUrl, resolveMistralVertexProject } from "./auth.js";
4
+ //#region src/vertex/index.ts
5
+ /**
6
+ * Creates a Mistral chat adapter that talks to Mistral on Vertex AI.
7
+ *
8
+ * Install `google-auth-library` next to `@tanstack/ai-mistral` for
9
+ * Application Default Credentials. Or pass `authClient` or `getAccessToken`.
10
+ */
11
+ function mistralVertexText(model, config = {}) {
12
+ const resolveRequestUrl = config.resolveRequestUrl ?? ((stream) => {
13
+ return `${resolveMistralVertexModelUrl(model, config)}:${stream ? "streamRawPredict" : "rawPredict"}`;
14
+ });
15
+ return new MistralTextAdapter({
16
+ apiKey: "vertex",
17
+ getAccessToken: () => resolveMistralVertexAccessToken(config),
18
+ resolveRequestUrl,
19
+ requestModel: model,
20
+ defaultHeaders: config.defaultHeaders
21
+ }, model);
22
+ }
23
+ //#endregion
24
+ export { MISTRAL_VERTEX_CHAT_MODELS, MistralVertexAuthError, mistralVertexText, resolveMistralVertexAccessToken, resolveMistralVertexLocation, resolveMistralVertexModelUrl, resolveMistralVertexProject };
25
+
26
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../../src/vertex/index.ts"],"sourcesContent":["import { MistralTextAdapter } from '../adapters/text'\nimport {\n resolveMistralVertexAccessToken,\n resolveMistralVertexModelUrl,\n} from './auth'\nimport type { MistralVertexChatModel } from '../model-meta'\nimport type { MistralVertexConfig } from './auth'\n\nexport {\n MistralVertexAuthError,\n resolveMistralVertexAccessToken,\n resolveMistralVertexLocation,\n resolveMistralVertexModelUrl,\n resolveMistralVertexProject,\n type MistralVertexConfig,\n type VertexAuthClient,\n} from './auth'\nexport {\n MISTRAL_VERTEX_CHAT_MODELS,\n type MistralVertexChatModel,\n} from '../model-meta'\n\n/**\n * Creates a Mistral chat adapter that talks to Mistral on Vertex AI.\n *\n * Install `google-auth-library` next to `@tanstack/ai-mistral` for\n * Application Default Credentials. Or pass `authClient` or `getAccessToken`.\n */\nexport function mistralVertexText<TModel extends MistralVertexChatModel>(\n model: TModel,\n config: MistralVertexConfig = {},\n): MistralTextAdapter<TModel> {\n const resolveRequestUrl =\n config.resolveRequestUrl ??\n ((stream: boolean) => {\n const modelUrl = resolveMistralVertexModelUrl(model, config)\n return `${modelUrl}:${stream ? 'streamRawPredict' : 'rawPredict'}`\n })\n\n return new MistralTextAdapter(\n {\n apiKey: 'vertex',\n getAccessToken: () => resolveMistralVertexAccessToken(config),\n resolveRequestUrl,\n requestModel: model,\n defaultHeaders: config.defaultHeaders,\n },\n model,\n )\n}\n"],"mappings":";;;;;;;;;;AA4BA,SAAgB,kBACd,OACA,SAA8B,CAAC,GACH;CAC5B,MAAM,oBACJ,OAAO,uBACL,WAAoB;EAEpB,OAAO,GADU,6BAA6B,OAAO,MAC3C,EAAS,GAAG,SAAS,qBAAqB;CACtD;CAEF,OAAO,IAAI,mBACT;EACE,QAAQ;EACR,sBAAsB,gCAAgC,MAAM;EAC5D;EACA,cAAc;EACd,gBAAgB,OAAO;CACzB,GACA,KACF;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/ai-mistral",
3
- "version": "0.3.4",
3
+ "version": "0.4.1",
4
4
  "type": "module",
5
5
  "description": "Mistral adapter for TanStack AI",
6
6
  "author": "",
@@ -24,6 +24,10 @@
24
24
  "./adapters/embedding": {
25
25
  "types": "./dist/esm/adapters/embedding.d.ts",
26
26
  "import": "./dist/esm/adapters/embedding.js"
27
+ },
28
+ "./vertex": {
29
+ "types": "./dist/esm/vertex/index.d.ts",
30
+ "import": "./dist/esm/vertex/index.js"
27
31
  }
28
32
  },
29
33
  "files": [
@@ -38,15 +42,23 @@
38
42
  ],
39
43
  "devDependencies": {
40
44
  "@vitest/coverage-v8": "4.1.10",
45
+ "google-auth-library": "^10.5.0",
41
46
  "vite": "^8.2.1",
42
- "@tanstack/ai": "0.47.1"
47
+ "@tanstack/ai": "0.48.0"
43
48
  },
44
49
  "peerDependencies": {
50
+ "google-auth-library": "^10.5.0",
45
51
  "zod": "^4.0.0",
46
- "@tanstack/ai": "^0.47.1"
52
+ "@tanstack/ai": "^0.48.0"
53
+ },
54
+ "peerDependenciesMeta": {
55
+ "google-auth-library": {
56
+ "optional": true
57
+ }
47
58
  },
48
59
  "dependencies": {
49
- "@mistralai/mistralai": "2.2.0"
60
+ "@mistralai/mistralai": "2.2.0",
61
+ "@tanstack/ai-utils": "0.4.0"
50
62
  },
51
63
  "scripts": {
52
64
  "build": "vite build",
@@ -1,25 +1,25 @@
1
1
  import { BaseTextAdapter } from '@tanstack/ai/adapters'
2
+ import { undoNullWidening } from '@tanstack/ai-utils'
2
3
  import { convertToolsToProviderFormat } from '../tools/tool-converter'
3
4
  import {
4
5
  createMistralClient,
5
6
  generateId,
6
7
  getMistralApiKeyFromEnv,
7
8
  } from '../utils/client'
8
- import {
9
- makeMistralStructuredOutputCompatible,
10
- transformNullsToUndefined,
11
- } from '../utils/schema-converter'
9
+ import { makeMistralStructuredOutputCompatibleWithMap } from '../utils/schema-converter'
10
+ import { createToolInputNormalizer } from '../utils/tool-input-normalizer'
12
11
  import type {
13
12
  ContentPart,
14
13
  Modality,
15
14
  ModelMessage,
16
- StreamChunk,
15
+ AdapterYieldChunk,
17
16
  TextOptions,
18
17
  } from '@tanstack/ai'
19
18
  import type {
20
19
  MISTRAL_CHAT_MODELS,
21
20
  MistralChatModelProviderOptionsByName,
22
21
  MistralModelInputModalitiesByName,
22
+ MistralTextAdapterModel,
23
23
  } from '../model-meta'
24
24
  import type {
25
25
  StructuredOutputOptions,
@@ -38,26 +38,30 @@ import type {
38
38
  MistralMessageMetadataByModality,
39
39
  } from '../message-types'
40
40
  import type { MistralClientConfig } from '../utils/client'
41
+ import type { ToolInputNormalizer } from '../utils/tool-input-normalizer'
41
42
 
42
43
  /** Cast an event object to StreamChunk. Adapters construct events with string
43
44
  * literal types which are structurally compatible with the EventType enum. */
44
45
  const asChunk = (chunk: Record<string, unknown>) =>
45
46
  // oxlint-disable-next-line eslint-js/no-restricted-syntax -- Record<string, unknown> doesn't structurally overlap the StreamChunk discriminated union; events are built with literal `type` fields the union accepts at runtime
46
- chunk as unknown as StreamChunk
47
+ chunk as unknown as AdapterYieldChunk
47
48
 
48
49
  /**
49
50
  * Parse the accumulated streaming arguments for a tool call. Throws a clear
50
51
  * error if the JSON is malformed — silently substituting `{}` would let a
51
52
  * tool fire with empty inputs, masking truncated streams or mis-shaped output.
52
53
  */
53
- function parseToolCallInput(toolCall: {
54
- id: string
55
- name: string
56
- arguments: string
57
- }): unknown {
54
+ function parseToolCallInput(
55
+ toolCall: {
56
+ id: string
57
+ name: string
58
+ arguments: string
59
+ },
60
+ normalizeToolInput: ToolInputNormalizer,
61
+ ): unknown {
58
62
  if (!toolCall.arguments) return {}
59
63
  try {
60
- return transformNullsToUndefined(JSON.parse(toolCall.arguments))
64
+ return normalizeToolInput(toolCall.name, JSON.parse(toolCall.arguments))
61
65
  } catch (cause) {
62
66
  const preview = toolCall.arguments.slice(0, 200)
63
67
  const ellipsis = toolCall.arguments.length > 200 ? '...' : ''
@@ -160,7 +164,7 @@ interface MistralRawChunk {
160
164
  * Tree-shakeable adapter for Mistral chat/text completion functionality.
161
165
  */
162
166
  export class MistralTextAdapter<
163
- TModel extends (typeof MISTRAL_CHAT_MODELS)[number],
167
+ TModel extends MistralTextAdapterModel,
164
168
  TProviderOptions extends Record<string, any> = ResolveProviderOptions<TModel>,
165
169
  TInputModalities extends ReadonlyArray<Modality> =
166
170
  ResolveInputModalities<TModel>,
@@ -186,7 +190,7 @@ export class MistralTextAdapter<
186
190
 
187
191
  async *chatStream(
188
192
  options: TextOptions<TProviderOptions>,
189
- ): AsyncIterable<StreamChunk> {
193
+ ): AsyncIterable<AdapterYieldChunk> {
190
194
  const requestParams = this.mapTextOptionsToMistral(options)
191
195
  const timestamp = Date.now()
192
196
 
@@ -242,7 +246,11 @@ export class MistralTextAdapter<
242
246
  const { stream: _stream, ...nonStreamParams } =
243
247
  this.mapTextOptionsToMistral(chatOptions)
244
248
 
245
- const jsonSchema = makeMistralStructuredOutputCompatible(
249
+ const {
250
+ schema: jsonSchema,
251
+ nullWideningMap,
252
+ strict,
253
+ } = makeMistralStructuredOutputCompatibleWithMap(
246
254
  outputSchema,
247
255
  outputSchema.required || [],
248
256
  )
@@ -254,7 +262,7 @@ export class MistralTextAdapter<
254
262
  jsonSchema: {
255
263
  name: 'structured_output',
256
264
  schemaDefinition: jsonSchema,
257
- strict: true,
265
+ strict,
258
266
  },
259
267
  },
260
268
  })
@@ -273,7 +281,7 @@ export class MistralTextAdapter<
273
281
 
274
282
  const usage = response.usage
275
283
  return {
276
- data: transformNullsToUndefined(parsed),
284
+ data: undoNullWidening(parsed, nullWideningMap),
277
285
  rawText: textContent,
278
286
  ...(usage && {
279
287
  usage: {
@@ -298,7 +306,7 @@ export class MistralTextAdapter<
298
306
  timestamp: number
299
307
  hasEmittedRunStarted: boolean
300
308
  },
301
- ): AsyncIterable<StreamChunk> {
309
+ ): AsyncIterable<AdapterYieldChunk> {
302
310
  let accumulatedContent = ''
303
311
  const timestamp = aguiState.timestamp
304
312
  let hasEmittedTextMessageStart = false
@@ -306,6 +314,7 @@ export class MistralTextAdapter<
306
314
  let hasEmittedToolCall = false
307
315
  let hasEmittedRunFinished = false
308
316
  let lastChunkModel = options.model
317
+ const normalizeToolInput = createToolInputNormalizer(options.tools)
309
318
 
310
319
  // Reasoning lifecycle (magistral-* models stream `thinking` content
311
320
  // parts before any text). Mirrors the anthropic adapter's pattern:
@@ -513,7 +522,10 @@ export class MistralTextAdapter<
513
522
  continue
514
523
  }
515
524
 
516
- const parsedInput = parseToolCallInput(toolCall)
525
+ const parsedInput = parseToolCallInput(
526
+ toolCall,
527
+ normalizeToolInput,
528
+ )
517
529
 
518
530
  toolCall.ended = true
519
531
  hasEmittedToolCall = true
@@ -615,7 +627,7 @@ export class MistralTextAdapter<
615
627
  toolName: toolCall.name,
616
628
  model: lastChunkModel,
617
629
  timestamp,
618
- input: parseToolCallInput(toolCall),
630
+ input: parseToolCallInput(toolCall, normalizeToolInput),
619
631
  })
620
632
  }
621
633
  }
@@ -676,7 +688,10 @@ export class MistralTextAdapter<
676
688
  let partialInput: unknown = {}
677
689
  try {
678
690
  partialInput = toolCall.arguments
679
- ? transformNullsToUndefined(JSON.parse(toolCall.arguments))
691
+ ? normalizeToolInput(
692
+ toolCall.name,
693
+ JSON.parse(toolCall.arguments),
694
+ )
680
695
  : {}
681
696
  } catch {
682
697
  partialInput = {}
@@ -708,13 +723,18 @@ export class MistralTextAdapter<
708
723
  const serverURL = (config.serverURL ?? 'https://api.mistral.ai')
709
724
  .replace(/\/+$/, '')
710
725
  .replace(/\/v1$/, '')
711
- const url = `${serverURL}/v1/chat/completions`
726
+ const url =
727
+ config.resolveRequestUrl?.(true) ?? `${serverURL}/v1/chat/completions`
712
728
 
713
729
  const body = this.toWireBody(params)
730
+ const accessToken =
731
+ config.getAccessToken === undefined
732
+ ? config.apiKey
733
+ : await config.getAccessToken()
714
734
 
715
735
  const headers: Record<string, string> = {
716
736
  'Content-Type': 'application/json',
717
- Authorization: `Bearer ${config.apiKey}`,
737
+ Authorization: `Bearer ${accessToken}`,
718
738
  ...config.defaultHeaders,
719
739
  }
720
740
 
@@ -906,7 +926,7 @@ export class MistralTextAdapter<
906
926
  }
907
927
 
908
928
  return {
909
- model: options.model,
929
+ model: this.rawConfig.requestModel ?? options.model,
910
930
  messages: messages,
911
931
  temperature: modelOptions?.temperature ?? undefined,
912
932
  maxTokens: modelOptions?.max_tokens ?? undefined,
@@ -1047,9 +1067,9 @@ export class MistralTextAdapter<
1047
1067
  * Normalizes message content to an array of ContentPart.
1048
1068
  */
1049
1069
  private normalizeContent(
1050
- content: string | null | Array<ContentPart>,
1070
+ content: string | null | undefined | Array<ContentPart>,
1051
1071
  ): Array<ContentPart> {
1052
- if (content === null) return []
1072
+ if (content === null || content === undefined) return []
1053
1073
  if (typeof content === 'string') return [{ type: 'text', content }]
1054
1074
  return content
1055
1075
  }
@@ -1058,9 +1078,9 @@ export class MistralTextAdapter<
1058
1078
  * Extracts text content from a content value that may be string, null, or ContentPart array.
1059
1079
  */
1060
1080
  private extractTextContent(
1061
- content: string | null | Array<ContentPart>,
1081
+ content: string | null | undefined | Array<ContentPart>,
1062
1082
  ): string {
1063
- if (content === null) return ''
1083
+ if (content === null || content === undefined) return ''
1064
1084
  if (typeof content === 'string') return content
1065
1085
  return content
1066
1086
  .filter((p) => p.type === 'text')
package/src/index.ts CHANGED
@@ -34,11 +34,16 @@ export type {
34
34
  ResolveProviderOptions,
35
35
  ResolveInputModalities,
36
36
  MistralChatModels,
37
+ MistralVertexChatModel,
37
38
  MistralEmbeddingModel,
38
39
  MistralEmbeddingModelProviderOptionsByName,
39
40
  MistralEmbeddingModelInputModalitiesByName,
40
41
  } from './model-meta'
41
- export { MISTRAL_CHAT_MODELS, MISTRAL_EMBEDDING_MODELS } from './model-meta'
42
+ export {
43
+ MISTRAL_CHAT_MODELS,
44
+ MISTRAL_EMBEDDING_MODELS,
45
+ MISTRAL_VERTEX_CHAT_MODELS,
46
+ } from './model-meta'
42
47
  export type {
43
48
  MistralTextMetadata,
44
49
  MistralImageMetadata,
package/src/model-meta.ts CHANGED
@@ -216,6 +216,56 @@ const OPEN_MISTRAL_NEMO = {
216
216
  },
217
217
  } as const satisfies ModelMeta<MistralTextProviderOptions>
218
218
 
219
+ // Vertex Model Garden IDs. These are not the Mistral API catalog.
220
+ // Source: https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/partner-models/mistral
221
+ const MISTRAL_MEDIUM_3 = {
222
+ name: 'mistral-medium-3',
223
+ context_window: 131_072,
224
+ max_completion_tokens: 8_192,
225
+ pricing: {
226
+ input: { normal: 0.4 },
227
+ output: { normal: 2 },
228
+ },
229
+ supports: {
230
+ input: ['text', 'image', 'document'],
231
+ output: ['text'],
232
+ endpoints: ['chat'],
233
+ features: ['streaming', 'tools', 'json_object', 'json_schema', 'vision'],
234
+ },
235
+ } as const satisfies ModelMeta<MistralTextProviderOptions>
236
+
237
+ const MISTRAL_SMALL_2503 = {
238
+ name: 'mistral-small-2503',
239
+ context_window: 131_072,
240
+ max_completion_tokens: 8_192,
241
+ pricing: {
242
+ input: { normal: 0.1 },
243
+ output: { normal: 0.3 },
244
+ },
245
+ supports: {
246
+ input: ['text', 'image', 'document'],
247
+ output: ['text'],
248
+ endpoints: ['chat'],
249
+ features: ['streaming', 'tools', 'json_object', 'json_schema', 'vision'],
250
+ },
251
+ } as const satisfies ModelMeta<MistralTextProviderOptions>
252
+
253
+ const CODESTRAL_2 = {
254
+ name: 'codestral-2',
255
+ context_window: 131_072,
256
+ max_completion_tokens: 8_192,
257
+ pricing: {
258
+ input: { normal: 0.3 },
259
+ output: { normal: 0.9 },
260
+ },
261
+ supports: {
262
+ input: ['text'],
263
+ output: ['text'],
264
+ endpoints: ['chat'],
265
+ features: ['streaming', 'tools', 'json_object', 'json_schema', 'code'],
266
+ },
267
+ } as const satisfies ModelMeta<MistralTextProviderOptions>
268
+
219
269
  /**
220
270
  * All supported Mistral chat model identifiers.
221
271
  */
@@ -238,6 +288,20 @@ export const MISTRAL_CHAT_MODELS = [
238
288
  */
239
289
  export type MistralChatModels = (typeof MISTRAL_CHAT_MODELS)[number]
240
290
 
291
+ /**
292
+ * Mistral chat models on Vertex AI / Gemini Enterprise Agent Platform.
293
+ * This list is the Google partner catalog, not the Mistral API catalog.
294
+ * OCR (`mistral-ocr-2505`) is not a chat model.
295
+ */
296
+ export const MISTRAL_VERTEX_CHAT_MODELS = [
297
+ MISTRAL_MEDIUM_3.name,
298
+ MISTRAL_SMALL_2503.name,
299
+ CODESTRAL_2.name,
300
+ ] as const
301
+
302
+ export type MistralVertexChatModel = (typeof MISTRAL_VERTEX_CHAT_MODELS)[number]
303
+ export type MistralTextAdapterModel = MistralChatModels | MistralVertexChatModel
304
+
241
305
  /**
242
306
  * Type-only map from Mistral chat model name to its supported input modalities.
243
307
  */
@@ -253,6 +317,9 @@ export type MistralModelInputModalitiesByName = {
253
317
  [MAGISTRAL_MEDIUM_LATEST.name]: typeof MAGISTRAL_MEDIUM_LATEST.supports.input
254
318
  [MAGISTRAL_SMALL_LATEST.name]: typeof MAGISTRAL_SMALL_LATEST.supports.input
255
319
  [OPEN_MISTRAL_NEMO.name]: typeof OPEN_MISTRAL_NEMO.supports.input
320
+ [MISTRAL_MEDIUM_3.name]: typeof MISTRAL_MEDIUM_3.supports.input
321
+ [MISTRAL_SMALL_2503.name]: typeof MISTRAL_SMALL_2503.supports.input
322
+ [CODESTRAL_2.name]: typeof CODESTRAL_2.supports.input
256
323
  }
257
324
 
258
325
  /**
@@ -270,6 +337,9 @@ export type MistralChatModelProviderOptionsByName = {
270
337
  [MAGISTRAL_MEDIUM_LATEST.name]: MistralReasoningProviderOptions
271
338
  [MAGISTRAL_SMALL_LATEST.name]: MistralReasoningProviderOptions
272
339
  [OPEN_MISTRAL_NEMO.name]: MistralTextProviderOptions
340
+ [MISTRAL_MEDIUM_3.name]: MistralVisionProviderOptions
341
+ [MISTRAL_SMALL_2503.name]: MistralVisionProviderOptions
342
+ [CODESTRAL_2.name]: MistralTextProviderOptions
273
343
  }
274
344
 
275
345
  /**
@@ -0,0 +1,32 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { convertFunctionToolToAdapterFormat } from './function-tool'
3
+
4
+ describe('convertFunctionToolToAdapterFormat', () => {
5
+ it('preserves composed schemas under the non-strict fallback', () => {
6
+ const inputSchema = {
7
+ type: 'object',
8
+ properties: {
9
+ value: {
10
+ oneOf: [{ type: 'string' }, { type: 'number' }],
11
+ },
12
+ },
13
+ required: [],
14
+ }
15
+
16
+ expect(
17
+ convertFunctionToolToAdapterFormat({
18
+ name: 'store_value',
19
+ description: 'Store a value',
20
+ inputSchema,
21
+ }),
22
+ ).toEqual({
23
+ type: 'function',
24
+ function: {
25
+ name: 'store_value',
26
+ description: 'Store a value',
27
+ parameters: inputSchema,
28
+ strict: false,
29
+ },
30
+ })
31
+ })
32
+ })
@@ -1,4 +1,4 @@
1
- import { makeMistralStructuredOutputCompatible } from '../utils/schema-converter'
1
+ import { makeMistralStructuredOutputCompatibleWithMap } from '../utils/schema-converter'
2
2
  import type { JSONSchema, Tool } from '@tanstack/ai'
3
3
  import type { ChatCompletionTool } from '../message-types'
4
4
 
@@ -7,11 +7,10 @@ export type FunctionTool = ChatCompletionTool
7
7
  /**
8
8
  * Converts a standard Tool to Mistral ChatCompletionTool format.
9
9
  *
10
- * Tool schemas are already converted to JSON Schema in the ai layer.
11
- * We apply Mistral-specific transformations for strict mode:
12
- * - All properties in required array
13
- * - Optional fields made nullable
14
- * - additionalProperties: false
10
+ * Tool schemas are already JSON Schema in the ai layer. When the schema can
11
+ * be inverted, rewrite it for strict mode (required, nullable optionals,
12
+ * `additionalProperties: false`) and set `strict: true`. Otherwise leave the
13
+ * schema intact and set `strict: false`.
15
14
  */
16
15
  export function convertFunctionToolToAdapterFormat(tool: Tool): FunctionTool {
17
16
  const baseSchema = (tool.inputSchema ?? {
@@ -25,10 +24,11 @@ export function convertFunctionToolToAdapterFormat(tool: Tool): FunctionTool {
25
24
  ? { ...baseSchema, properties: {} }
26
25
  : { ...baseSchema }
27
26
 
28
- const jsonSchema = makeMistralStructuredOutputCompatible(
29
- inputSchema,
30
- inputSchema.required || [],
31
- )
27
+ const { schema: jsonSchema, strict } =
28
+ makeMistralStructuredOutputCompatibleWithMap(
29
+ inputSchema,
30
+ inputSchema.required || [],
31
+ )
32
32
 
33
33
  return {
34
34
  type: 'function',
@@ -36,7 +36,7 @@ export function convertFunctionToolToAdapterFormat(tool: Tool): FunctionTool {
36
36
  name: tool.name,
37
37
  description: tool.description,
38
38
  parameters: jsonSchema,
39
- strict: true,
39
+ strict,
40
40
  },
41
41
  } satisfies FunctionTool
42
42
  }
@@ -12,22 +12,57 @@ export interface MistralClientConfig {
12
12
 
13
13
  /** Optional default headers to include with every request. */
14
14
  defaultHeaders?: Record<string, string>
15
+
16
+ /**
17
+ * Optional Google / Vertex access token. When set, it replaces
18
+ * `apiKey` on the Authorization header.
19
+ */
20
+ getAccessToken?: () => Promise<string>
21
+
22
+ /**
23
+ * Optional chat completions URL. Vertex uses this for
24
+ * `:rawPredict` and `:streamRawPredict`.
25
+ */
26
+ resolveRequestUrl?: (stream: boolean) => string
27
+
28
+ /** Optional model id sent on the wire. Vertex uses publisher model ids. */
29
+ requestModel?: string
15
30
  }
16
31
 
17
32
  /**
18
33
  * Creates a Mistral SDK client instance.
19
34
  */
20
35
  export function createMistralClient(config: MistralClientConfig): Mistral {
21
- const { apiKey, serverURL, timeoutMs, defaultHeaders } = config
36
+ const {
37
+ apiKey,
38
+ serverURL,
39
+ timeoutMs,
40
+ defaultHeaders,
41
+ getAccessToken,
42
+ resolveRequestUrl,
43
+ } = config
44
+
45
+ const needsHook =
46
+ (defaultHeaders !== undefined && Object.keys(defaultHeaders).length > 0) ||
47
+ getAccessToken !== undefined ||
48
+ resolveRequestUrl !== undefined
22
49
 
23
50
  let httpClient: HTTPClient | undefined
24
- if (defaultHeaders && Object.keys(defaultHeaders).length > 0) {
51
+ if (needsHook) {
25
52
  httpClient = new HTTPClient()
26
- httpClient.addHook('beforeRequest', (req) => {
27
- for (const [key, value] of Object.entries(defaultHeaders)) {
28
- req.headers.set(key, value)
53
+ httpClient.addHook('beforeRequest', async (req) => {
54
+ const nextUrl =
55
+ resolveRequestUrl === undefined ? req.url : resolveRequestUrl(false)
56
+ const next = new Request(nextUrl, req)
57
+ if (defaultHeaders) {
58
+ for (const [key, value] of Object.entries(defaultHeaders)) {
59
+ next.headers.set(key, value)
60
+ }
61
+ }
62
+ if (getAccessToken !== undefined) {
63
+ next.headers.set('Authorization', `Bearer ${await getAccessToken()}`)
29
64
  }
30
- return req
65
+ return next
31
66
  })
32
67
  }
33
68