@tanstack/ai-mistral 0.1.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 (38) hide show
  1. package/README.md +93 -0
  2. package/dist/esm/adapters/text.d.ts +106 -0
  3. package/dist/esm/adapters/text.js +741 -0
  4. package/dist/esm/adapters/text.js.map +1 -0
  5. package/dist/esm/index.d.ts +10 -0
  6. package/dist/esm/index.js +9 -0
  7. package/dist/esm/index.js.map +1 -0
  8. package/dist/esm/message-types.d.ts +155 -0
  9. package/dist/esm/model-meta.d.ts +263 -0
  10. package/dist/esm/model-meta.js +50 -0
  11. package/dist/esm/model-meta.js.map +1 -0
  12. package/dist/esm/text/text-provider-options.d.ts +80 -0
  13. package/dist/esm/tools/function-tool.d.ts +13 -0
  14. package/dist/esm/tools/function-tool.js +26 -0
  15. package/dist/esm/tools/function-tool.js.map +1 -0
  16. package/dist/esm/tools/index.d.ts +2 -0
  17. package/dist/esm/tools/tool-converter.d.ts +6 -0
  18. package/dist/esm/tools/tool-converter.js +10 -0
  19. package/dist/esm/tools/tool-converter.js.map +1 -0
  20. package/dist/esm/utils/client.d.ts +24 -0
  21. package/dist/esm/utils/client.js +44 -0
  22. package/dist/esm/utils/client.js.map +1 -0
  23. package/dist/esm/utils/index.d.ts +2 -0
  24. package/dist/esm/utils/schema-converter.d.ts +18 -0
  25. package/dist/esm/utils/schema-converter.js +93 -0
  26. package/dist/esm/utils/schema-converter.js.map +1 -0
  27. package/package.json +60 -0
  28. package/src/adapters/text.ts +1141 -0
  29. package/src/index.ts +33 -0
  30. package/src/message-types.ts +194 -0
  31. package/src/model-meta.ts +285 -0
  32. package/src/text/text-provider-options.ts +103 -0
  33. package/src/tools/function-tool.ts +42 -0
  34. package/src/tools/index.ts +5 -0
  35. package/src/tools/tool-converter.ts +14 -0
  36. package/src/utils/client.ts +70 -0
  37. package/src/utils/index.ts +10 -0
  38. package/src/utils/schema-converter.ts +136 -0
package/src/index.ts ADDED
@@ -0,0 +1,33 @@
1
+ /**
2
+ * @module @tanstack/ai-mistral
3
+ *
4
+ * Mistral provider adapter for TanStack AI.
5
+ * Provides tree-shakeable adapters for Mistral's Chat Completions API.
6
+ */
7
+
8
+ // Text (Chat) adapter
9
+ export {
10
+ MistralTextAdapter,
11
+ createMistralText,
12
+ mistralText,
13
+ type MistralTextConfig,
14
+ type MistralTextProviderOptions,
15
+ } from './adapters/text'
16
+
17
+ // Types
18
+ export type {
19
+ MistralChatModelProviderOptionsByName,
20
+ MistralModelInputModalitiesByName,
21
+ ResolveProviderOptions,
22
+ ResolveInputModalities,
23
+ MistralChatModels,
24
+ } from './model-meta'
25
+ export { MISTRAL_CHAT_MODELS } from './model-meta'
26
+ export type {
27
+ MistralTextMetadata,
28
+ MistralImageMetadata,
29
+ MistralAudioMetadata,
30
+ MistralVideoMetadata,
31
+ MistralDocumentMetadata,
32
+ MistralMessageMetadataByModality,
33
+ } from './message-types'
@@ -0,0 +1,194 @@
1
+ /**
2
+ * Mistral-specific message types for the Chat Completions API.
3
+ *
4
+ * These types mirror the shape expected by the Mistral SDK (`@mistralai/mistralai`)
5
+ * and are used internally by the adapter to avoid tight coupling to the SDK's
6
+ * exported types.
7
+ *
8
+ * @see https://docs.mistral.ai/api/
9
+ */
10
+
11
+ export interface ChatCompletionContentPartText {
12
+ /** The text content. */
13
+ text: string
14
+
15
+ /** The type of the content part. */
16
+ type: 'text'
17
+ }
18
+
19
+ export interface ChatCompletionContentPartImage {
20
+ imageUrl: string | { url: string; detail?: 'auto' | 'low' | 'high' }
21
+
22
+ /** The type of the content part. */
23
+ type: 'image_url'
24
+ }
25
+
26
+ export interface ChatCompletionContentPartDocumentUrl {
27
+ documentUrl: string
28
+
29
+ /** The type of the content part. */
30
+ type: 'document_url'
31
+ }
32
+
33
+ export type ChatCompletionContentPart =
34
+ | ChatCompletionContentPartText
35
+ | ChatCompletionContentPartImage
36
+ | ChatCompletionContentPartDocumentUrl
37
+
38
+ export interface ChatCompletionMessageToolCall {
39
+ /** The ID of the tool call. */
40
+ id: string
41
+
42
+ /** The type of the tool. Currently only `function` is supported. */
43
+ type?: 'function'
44
+
45
+ /** The function that the model called. */
46
+ function: {
47
+ /** The name of the function to call. */
48
+ name: string
49
+
50
+ /** Arguments generated by the model as a JSON string. */
51
+ arguments: string
52
+ }
53
+ }
54
+
55
+ export type FunctionParameters = { [key: string]: unknown }
56
+
57
+ export interface FunctionDefinition {
58
+ /**
59
+ * The name of the function. Must be a-z, A-Z, 0-9, or contain underscores and
60
+ * dashes, with a maximum length of 64.
61
+ */
62
+ name: string
63
+
64
+ /** A description of what the function does. */
65
+ description?: string
66
+
67
+ /** Function parameters defined as a JSON Schema object. */
68
+ parameters?: FunctionParameters
69
+
70
+ /** Whether to enable strict schema adherence. */
71
+ strict?: boolean
72
+ }
73
+
74
+ export interface ChatCompletionTool {
75
+ /** The type of the tool. */
76
+ type: 'function'
77
+
78
+ function: FunctionDefinition
79
+ }
80
+
81
+ export interface ChatCompletionNamedToolChoice {
82
+ type: 'function'
83
+ function: {
84
+ name: string
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Controls which (if any) tool is called by the model.
90
+ *
91
+ * - `none` — never call tools
92
+ * - `auto` — model decides
93
+ * - `any` / `required` — model must call one or more tools
94
+ * - Named tool choice — forces a specific tool
95
+ */
96
+ export type ChatCompletionToolChoiceOption =
97
+ | 'none'
98
+ | 'auto'
99
+ | 'any'
100
+ | 'required'
101
+ | ChatCompletionNamedToolChoice
102
+
103
+ export interface ChatCompletionSystemMessageParam {
104
+ role: 'system'
105
+ content: string | Array<ChatCompletionContentPartText>
106
+ }
107
+
108
+ export interface ChatCompletionUserMessageParam {
109
+ role: 'user'
110
+ content: string | Array<ChatCompletionContentPart>
111
+ }
112
+
113
+ export interface ChatCompletionAssistantMessageParam {
114
+ role: 'assistant'
115
+ content?: string | Array<ChatCompletionContentPartText> | null
116
+ toolCalls?: Array<ChatCompletionMessageToolCall>
117
+ prefix?: boolean
118
+ }
119
+
120
+ export interface ChatCompletionToolMessageParam {
121
+ role: 'tool'
122
+ content: string | Array<ChatCompletionContentPartText>
123
+ toolCallId: string
124
+ name?: string
125
+ }
126
+
127
+ export type ChatCompletionMessageParam =
128
+ | ChatCompletionSystemMessageParam
129
+ | ChatCompletionUserMessageParam
130
+ | ChatCompletionAssistantMessageParam
131
+ | ChatCompletionToolMessageParam
132
+
133
+ export interface ResponseFormatText {
134
+ type: 'text'
135
+ }
136
+
137
+ export interface ResponseFormatJsonObject {
138
+ type: 'json_object'
139
+ }
140
+
141
+ export interface ResponseFormatJsonSchema {
142
+ type: 'json_schema'
143
+ jsonSchema: {
144
+ name: string
145
+ description?: string
146
+ schemaDefinition: { [key: string]: unknown }
147
+ strict?: boolean
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Metadata for Mistral text content parts.
153
+ */
154
+ export type MistralTextMetadata = Record<string, never>
155
+
156
+ /**
157
+ * Metadata for Mistral image content parts.
158
+ */
159
+ export interface MistralImageMetadata {
160
+ /**
161
+ * Specifies the detail level of the image.
162
+ * @default 'auto'
163
+ */
164
+ detail?: 'auto' | 'low' | 'high'
165
+ }
166
+
167
+ /**
168
+ * Metadata for Mistral audio content parts.
169
+ * Mistral does not currently support audio input.
170
+ */
171
+ export type MistralAudioMetadata = Record<string, never>
172
+
173
+ /**
174
+ * Metadata for Mistral video content parts.
175
+ * Mistral does not currently support video input.
176
+ */
177
+ export type MistralVideoMetadata = Record<string, never>
178
+
179
+ /**
180
+ * Metadata for Mistral document content parts.
181
+ * Used with document understanding models via `document_url` parts.
182
+ */
183
+ export type MistralDocumentMetadata = Record<string, never>
184
+
185
+ /**
186
+ * Map of modality types to their Mistral-specific metadata types.
187
+ */
188
+ export interface MistralMessageMetadataByModality {
189
+ text: MistralTextMetadata
190
+ image: MistralImageMetadata
191
+ audio: MistralAudioMetadata
192
+ video: MistralVideoMetadata
193
+ document: MistralDocumentMetadata
194
+ }
@@ -0,0 +1,285 @@
1
+ import type { MistralTextProviderOptions } from './text/text-provider-options'
2
+
3
+ /** Provider options for vision-capable Mistral models (pixtral-*). */
4
+ export type MistralVisionProviderOptions = MistralTextProviderOptions
5
+
6
+ /** Provider options for reasoning-capable Mistral models (magistral-*). */
7
+ export type MistralReasoningProviderOptions = MistralTextProviderOptions
8
+
9
+ /**
10
+ * Internal metadata structure describing a Mistral model's capabilities
11
+ * and approximate pricing (USD per million tokens).
12
+ */
13
+ interface ModelMeta<TProviderOptions = unknown> {
14
+ name: string
15
+ context_window?: number
16
+ max_completion_tokens?: number
17
+ pricing: {
18
+ input?: { normal: number; cached?: number }
19
+ output?: { normal: number }
20
+ }
21
+ supports: {
22
+ input: Array<'text' | 'image' | 'audio'>
23
+ output: Array<'text'>
24
+ endpoints: Array<'chat' | 'embeddings'>
25
+
26
+ features: Array<
27
+ | 'streaming'
28
+ | 'tools'
29
+ | 'json_object'
30
+ | 'json_schema'
31
+ | 'reasoning'
32
+ | 'vision'
33
+ | 'code'
34
+ >
35
+ }
36
+ providerOptions?: TProviderOptions
37
+ }
38
+
39
+ const MISTRAL_LARGE_LATEST = {
40
+ name: 'mistral-large-latest',
41
+ context_window: 131_072,
42
+ max_completion_tokens: 8_192,
43
+ pricing: {
44
+ input: { normal: 0.5 },
45
+ output: { normal: 1.5 },
46
+ },
47
+ supports: {
48
+ input: ['text'],
49
+ output: ['text'],
50
+ endpoints: ['chat'],
51
+ features: ['streaming', 'tools', 'json_object', 'json_schema'],
52
+ },
53
+ } as const satisfies ModelMeta<MistralTextProviderOptions>
54
+
55
+ const MISTRAL_MEDIUM_LATEST = {
56
+ name: 'mistral-medium-latest',
57
+ context_window: 131_072,
58
+ max_completion_tokens: 8_192,
59
+ pricing: {
60
+ input: { normal: 0.4 },
61
+ output: { normal: 2 },
62
+ },
63
+ supports: {
64
+ input: ['text', 'image'],
65
+ output: ['text'],
66
+ endpoints: ['chat'],
67
+ features: ['streaming', 'tools', 'json_object', 'json_schema', 'vision'],
68
+ },
69
+ } as const satisfies ModelMeta<MistralTextProviderOptions>
70
+
71
+ const MISTRAL_SMALL_LATEST = {
72
+ name: 'mistral-small-latest',
73
+ context_window: 131_072,
74
+ max_completion_tokens: 8_192,
75
+ pricing: {
76
+ input: { normal: 0.1 },
77
+ output: { normal: 0.3 },
78
+ },
79
+ supports: {
80
+ input: ['text', 'image'],
81
+ output: ['text'],
82
+ endpoints: ['chat'],
83
+ features: ['streaming', 'tools', 'json_object', 'json_schema', 'vision'],
84
+ },
85
+ } as const satisfies ModelMeta<MistralTextProviderOptions>
86
+
87
+ const MINISTRAL_8B_LATEST = {
88
+ name: 'ministral-8b-latest',
89
+ context_window: 131_072,
90
+ max_completion_tokens: 8_192,
91
+ pricing: {
92
+ input: { normal: 0.1 },
93
+ output: { normal: 0.1 },
94
+ },
95
+ supports: {
96
+ input: ['text'],
97
+ output: ['text'],
98
+ endpoints: ['chat'],
99
+ features: ['streaming', 'tools', 'json_object', 'json_schema'],
100
+ },
101
+ } as const satisfies ModelMeta<MistralTextProviderOptions>
102
+
103
+ const MINISTRAL_3B_LATEST = {
104
+ name: 'ministral-3b-latest',
105
+ context_window: 131_072,
106
+ max_completion_tokens: 8_192,
107
+ pricing: {
108
+ input: { normal: 0.04 },
109
+ output: { normal: 0.04 },
110
+ },
111
+ supports: {
112
+ input: ['text'],
113
+ output: ['text'],
114
+ endpoints: ['chat'],
115
+ features: ['streaming', 'tools', 'json_object', 'json_schema'],
116
+ },
117
+ } as const satisfies ModelMeta<MistralTextProviderOptions>
118
+
119
+ const CODESTRAL_LATEST = {
120
+ name: 'codestral-latest',
121
+ context_window: 256_000,
122
+ max_completion_tokens: 8_192,
123
+ pricing: {
124
+ input: { normal: 0.3 },
125
+ output: { normal: 0.9 },
126
+ },
127
+ supports: {
128
+ input: ['text'],
129
+ output: ['text'],
130
+ endpoints: ['chat'],
131
+ features: ['streaming', 'tools', 'json_object', 'json_schema', 'code'],
132
+ },
133
+ } as const satisfies ModelMeta<MistralTextProviderOptions>
134
+
135
+ const PIXTRAL_LARGE_LATEST = {
136
+ name: 'pixtral-large-latest',
137
+ context_window: 131_072,
138
+ max_completion_tokens: 8_192,
139
+ pricing: {
140
+ input: { normal: 2 },
141
+ output: { normal: 6 },
142
+ },
143
+ supports: {
144
+ input: ['text', 'image'],
145
+ output: ['text'],
146
+ endpoints: ['chat'],
147
+ features: ['streaming', 'tools', 'json_object', 'json_schema', 'vision'],
148
+ },
149
+ } as const satisfies ModelMeta<MistralTextProviderOptions>
150
+
151
+ const PIXTRAL_12B_2409 = {
152
+ name: 'pixtral-12b-2409',
153
+ context_window: 131_072,
154
+ max_completion_tokens: 8_192,
155
+ pricing: {
156
+ input: { normal: 0.15 },
157
+ output: { normal: 0.15 },
158
+ },
159
+ supports: {
160
+ input: ['text', 'image'],
161
+ output: ['text'],
162
+ endpoints: ['chat'],
163
+ features: ['streaming', 'tools', 'json_object', 'vision'],
164
+ },
165
+ } as const satisfies ModelMeta<MistralTextProviderOptions>
166
+
167
+ const MAGISTRAL_MEDIUM_LATEST = {
168
+ name: 'magistral-medium-latest',
169
+ context_window: 40_000,
170
+ max_completion_tokens: 40_000,
171
+ pricing: {
172
+ input: { normal: 2 },
173
+ output: { normal: 5 },
174
+ },
175
+ supports: {
176
+ input: ['text'],
177
+ output: ['text'],
178
+ endpoints: ['chat'],
179
+ features: ['streaming', 'tools', 'reasoning', 'json_object', 'json_schema'],
180
+ },
181
+ } as const satisfies ModelMeta<MistralTextProviderOptions>
182
+
183
+ const MAGISTRAL_SMALL_LATEST = {
184
+ name: 'magistral-small-latest',
185
+ context_window: 40_000,
186
+ max_completion_tokens: 40_000,
187
+ pricing: {
188
+ input: { normal: 0.5 },
189
+ output: { normal: 1.5 },
190
+ },
191
+ supports: {
192
+ input: ['text'],
193
+ output: ['text'],
194
+ endpoints: ['chat'],
195
+ features: ['streaming', 'tools', 'reasoning', 'json_object', 'json_schema'],
196
+ },
197
+ } as const satisfies ModelMeta<MistralTextProviderOptions>
198
+
199
+ const OPEN_MISTRAL_NEMO = {
200
+ name: 'open-mistral-nemo',
201
+ context_window: 131_072,
202
+ max_completion_tokens: 8_192,
203
+ pricing: {
204
+ input: { normal: 0.15 },
205
+ output: { normal: 0.15 },
206
+ },
207
+ supports: {
208
+ input: ['text'],
209
+ output: ['text'],
210
+ endpoints: ['chat'],
211
+ features: ['streaming', 'tools', 'json_object'],
212
+ },
213
+ } as const satisfies ModelMeta<MistralTextProviderOptions>
214
+
215
+ /**
216
+ * All supported Mistral chat model identifiers.
217
+ */
218
+ export const MISTRAL_CHAT_MODELS = [
219
+ MISTRAL_LARGE_LATEST.name,
220
+ MISTRAL_MEDIUM_LATEST.name,
221
+ MISTRAL_SMALL_LATEST.name,
222
+ MINISTRAL_8B_LATEST.name,
223
+ MINISTRAL_3B_LATEST.name,
224
+ CODESTRAL_LATEST.name,
225
+ PIXTRAL_LARGE_LATEST.name,
226
+ PIXTRAL_12B_2409.name,
227
+ MAGISTRAL_MEDIUM_LATEST.name,
228
+ MAGISTRAL_SMALL_LATEST.name,
229
+ OPEN_MISTRAL_NEMO.name,
230
+ ] as const
231
+
232
+ /**
233
+ * Union type of all supported Mistral chat model names.
234
+ */
235
+ export type MistralChatModels = (typeof MISTRAL_CHAT_MODELS)[number]
236
+
237
+ /**
238
+ * Type-only map from Mistral chat model name to its supported input modalities.
239
+ */
240
+ export type MistralModelInputModalitiesByName = {
241
+ [MISTRAL_LARGE_LATEST.name]: typeof MISTRAL_LARGE_LATEST.supports.input
242
+ [MISTRAL_MEDIUM_LATEST.name]: typeof MISTRAL_MEDIUM_LATEST.supports.input
243
+ [MISTRAL_SMALL_LATEST.name]: typeof MISTRAL_SMALL_LATEST.supports.input
244
+ [MINISTRAL_8B_LATEST.name]: typeof MINISTRAL_8B_LATEST.supports.input
245
+ [MINISTRAL_3B_LATEST.name]: typeof MINISTRAL_3B_LATEST.supports.input
246
+ [CODESTRAL_LATEST.name]: typeof CODESTRAL_LATEST.supports.input
247
+ [PIXTRAL_LARGE_LATEST.name]: typeof PIXTRAL_LARGE_LATEST.supports.input
248
+ [PIXTRAL_12B_2409.name]: typeof PIXTRAL_12B_2409.supports.input
249
+ [MAGISTRAL_MEDIUM_LATEST.name]: typeof MAGISTRAL_MEDIUM_LATEST.supports.input
250
+ [MAGISTRAL_SMALL_LATEST.name]: typeof MAGISTRAL_SMALL_LATEST.supports.input
251
+ [OPEN_MISTRAL_NEMO.name]: typeof OPEN_MISTRAL_NEMO.supports.input
252
+ }
253
+
254
+ /**
255
+ * Type-only map from Mistral chat model name to its provider options type.
256
+ */
257
+ export type MistralChatModelProviderOptionsByName = {
258
+ [MISTRAL_LARGE_LATEST.name]: MistralTextProviderOptions
259
+ [MISTRAL_MEDIUM_LATEST.name]: MistralVisionProviderOptions
260
+ [MISTRAL_SMALL_LATEST.name]: MistralVisionProviderOptions
261
+ [MINISTRAL_8B_LATEST.name]: MistralTextProviderOptions
262
+ [MINISTRAL_3B_LATEST.name]: MistralTextProviderOptions
263
+ [CODESTRAL_LATEST.name]: MistralTextProviderOptions
264
+ [PIXTRAL_LARGE_LATEST.name]: MistralVisionProviderOptions
265
+ [PIXTRAL_12B_2409.name]: MistralVisionProviderOptions
266
+ [MAGISTRAL_MEDIUM_LATEST.name]: MistralReasoningProviderOptions
267
+ [MAGISTRAL_SMALL_LATEST.name]: MistralReasoningProviderOptions
268
+ [OPEN_MISTRAL_NEMO.name]: MistralTextProviderOptions
269
+ }
270
+
271
+ /**
272
+ * Resolves the provider options type for a specific Mistral model.
273
+ */
274
+ export type ResolveProviderOptions<TModel extends string> =
275
+ TModel extends keyof MistralChatModelProviderOptionsByName
276
+ ? MistralChatModelProviderOptionsByName[TModel]
277
+ : MistralTextProviderOptions
278
+
279
+ /**
280
+ * Resolve input modalities for a specific model.
281
+ */
282
+ export type ResolveInputModalities<TModel extends string> =
283
+ TModel extends keyof MistralModelInputModalitiesByName
284
+ ? MistralModelInputModalitiesByName[TModel]
285
+ : readonly ['text']
@@ -0,0 +1,103 @@
1
+ import type {
2
+ ChatCompletionMessageParam,
3
+ ChatCompletionTool,
4
+ ChatCompletionToolChoiceOption,
5
+ ResponseFormatJsonObject,
6
+ ResponseFormatJsonSchema,
7
+ ResponseFormatText,
8
+ } from '../message-types'
9
+
10
+ /**
11
+ * Mistral-specific provider options for text/chat models.
12
+ *
13
+ * @see https://docs.mistral.ai/api/
14
+ */
15
+ export interface MistralTextProviderOptions {
16
+ /**
17
+ * Sampling temperature. The default varies by model; lower values make output
18
+ * more deterministic. We recommend altering this OR `top_p`, not both.
19
+ */
20
+ temperature?: number | null
21
+
22
+ /**
23
+ * Nucleus sampling — consider the tokens with `top_p` probability mass.
24
+ */
25
+ top_p?: number | null
26
+
27
+ /**
28
+ * The maximum number of tokens to generate.
29
+ */
30
+ max_tokens?: number | null
31
+
32
+ /**
33
+ * Stop sequences where the API will stop generating further tokens.
34
+ */
35
+ stop?: string | Array<string> | null
36
+
37
+ /**
38
+ * A seed for deterministic sampling. Repeated requests with the same seed
39
+ * and parameters should return the same result (best-effort).
40
+ */
41
+ random_seed?: number | null
42
+
43
+ /**
44
+ * Specifies the format the model must output.
45
+ */
46
+ response_format?:
47
+ | ResponseFormatText
48
+ | ResponseFormatJsonSchema
49
+ | ResponseFormatJsonObject
50
+ | null
51
+
52
+ /**
53
+ * Controls which (if any) tool is called by the model.
54
+ */
55
+ tool_choice?: ChatCompletionToolChoiceOption | null
56
+
57
+ /**
58
+ * Whether parallel tool calls are allowed during tool use.
59
+ */
60
+ parallel_tool_calls?: boolean | null
61
+
62
+ /**
63
+ * Number between -2.0 and 2.0. Positive values penalize tokens based on
64
+ * their frequency in the text so far.
65
+ */
66
+ frequency_penalty?: number | null
67
+
68
+ /**
69
+ * Number between -2.0 and 2.0. Positive values penalize tokens based on
70
+ * whether they appear in the text so far.
71
+ */
72
+ presence_penalty?: number | null
73
+
74
+ /**
75
+ * How many chat completion choices to generate for each input message.
76
+ */
77
+ n?: number | null
78
+
79
+ /**
80
+ * Prediction — used to speed up generation with speculative decoding.
81
+ */
82
+ prediction?: { type: 'content'; content: string } | null
83
+
84
+ /**
85
+ * Safe prompt — enables safety guarding injected into the system prompt.
86
+ */
87
+ safe_prompt?: boolean | null
88
+ }
89
+
90
+ /**
91
+ * Internal options interface used for validation within the adapter.
92
+ */
93
+ export interface InternalTextProviderOptions extends MistralTextProviderOptions {
94
+ messages: Array<ChatCompletionMessageParam>
95
+ model: string
96
+ stream?: boolean | null
97
+ tools?: Array<ChatCompletionTool>
98
+ }
99
+
100
+ /**
101
+ * External provider options (what users pass in).
102
+ */
103
+ export type ExternalTextProviderOptions = MistralTextProviderOptions
@@ -0,0 +1,42 @@
1
+ import { makeMistralStructuredOutputCompatible } from '../utils/schema-converter'
2
+ import type { JSONSchema, Tool } from '@tanstack/ai'
3
+ import type { ChatCompletionTool } from '../message-types'
4
+
5
+ export type FunctionTool = ChatCompletionTool
6
+
7
+ /**
8
+ * Converts a standard Tool to Mistral ChatCompletionTool format.
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
15
+ */
16
+ export function convertFunctionToolToAdapterFormat(tool: Tool): FunctionTool {
17
+ const baseSchema = (tool.inputSchema ?? {
18
+ type: 'object',
19
+ properties: {},
20
+ required: [],
21
+ }) as JSONSchema
22
+
23
+ const inputSchema: JSONSchema =
24
+ baseSchema.type === 'object' && !baseSchema.properties
25
+ ? { ...baseSchema, properties: {} }
26
+ : { ...baseSchema }
27
+
28
+ const jsonSchema = makeMistralStructuredOutputCompatible(
29
+ inputSchema,
30
+ inputSchema.required || [],
31
+ )
32
+
33
+ return {
34
+ type: 'function',
35
+ function: {
36
+ name: tool.name,
37
+ description: tool.description,
38
+ parameters: jsonSchema,
39
+ strict: true,
40
+ },
41
+ } satisfies FunctionTool
42
+ }
@@ -0,0 +1,5 @@
1
+ export {
2
+ convertFunctionToolToAdapterFormat,
3
+ type FunctionTool,
4
+ } from './function-tool'
5
+ export { convertToolsToProviderFormat } from './tool-converter'
@@ -0,0 +1,14 @@
1
+ import { convertFunctionToolToAdapterFormat } from './function-tool'
2
+ import type { FunctionTool } from './function-tool'
3
+ import type { Tool } from '@tanstack/ai'
4
+
5
+ /**
6
+ * Converts an array of standard Tools to Mistral-specific format.
7
+ */
8
+ export function convertToolsToProviderFormat(
9
+ tools: Array<Tool>,
10
+ ): Array<FunctionTool> {
11
+ return tools.map((tool) => {
12
+ return convertFunctionToolToAdapterFormat(tool)
13
+ })
14
+ }