@tanstack/ai-mistral 1.1.0 → 1.1.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.
@@ -0,0 +1,588 @@
1
+ import { MistralError } from '@mistralai/mistralai/models/errors/mistralerror.js'
2
+ import { SDKError } from '@mistralai/mistralai/models/errors/sdkerror.js'
3
+ import {
4
+ ConnectionError,
5
+ RequestAbortedError,
6
+ RequestTimeoutError,
7
+ UnexpectedClientError,
8
+ } from '@mistralai/mistralai/models/errors/httpclienterrors.js'
9
+ import type {
10
+ AssistantMessage,
11
+ AssistantMessage$Outbound,
12
+ ChatCompletionChoice,
13
+ ChatCompletionRequest,
14
+ ChatCompletionResponse,
15
+ ContentChunk,
16
+ ContentChunk$Outbound,
17
+ EmbeddingRequest,
18
+ EmbeddingResponse,
19
+ EmbeddingResponseData,
20
+ ImageURLChunk,
21
+ ThinkChunk,
22
+ Thinking$Outbound,
23
+ ToolCall,
24
+ UsageInfo,
25
+ UsageInfo$Outbound,
26
+ ValidationError,
27
+ } from '@mistralai/mistralai/models/components'
28
+ import type { MistralClientConfig } from '../utils/client'
29
+
30
+ /**
31
+ * The part of the Mistral SDK client that the adapters call. A real
32
+ * `Mistral` instance fits this type.
33
+ */
34
+ export interface MistralClient {
35
+ chat: {
36
+ complete: (
37
+ request: ChatCompletionRequest,
38
+ options?: MistralRequestOptions,
39
+ ) => Promise<ChatCompletionResponse>
40
+ }
41
+ embeddings: {
42
+ create: (
43
+ request: EmbeddingRequest,
44
+ options?: MistralRequestOptions,
45
+ ) => Promise<EmbeddingResponse>
46
+ }
47
+ }
48
+
49
+ /**
50
+ * The part of the SDK `RequestOptions` that the adapters pass. Same as the
51
+ * SDK: a signal replaces the default timeout.
52
+ */
53
+ export interface MistralRequestOptions {
54
+ signal?: AbortSignal | null
55
+ fetchOptions?: { signal?: AbortSignal | null }
56
+ }
57
+
58
+ type Fetcher = (url: string, init: RequestInit) => Promise<Response>
59
+
60
+ // ===========================
61
+ // Outbound: SDK camelCase -> wire snake_case (the SDK `$outboundSchema`s)
62
+ // ===========================
63
+
64
+ function chunkToWire(chunk: ContentChunk) {
65
+ switch (chunk.type) {
66
+ case 'image_url':
67
+ return { type: chunk.type, image_url: chunk.imageUrl }
68
+ case 'document_url':
69
+ return {
70
+ type: chunk.type,
71
+ document_url: chunk.documentUrl,
72
+ document_name: chunk.documentName,
73
+ }
74
+ case 'text':
75
+ return { type: chunk.type, text: chunk.text }
76
+ case 'reference':
77
+ return { type: chunk.type, reference_ids: chunk.referenceIds }
78
+ case 'file':
79
+ return { type: chunk.type, file_id: chunk.fileId }
80
+ case 'thinking':
81
+ return thinkChunkToWire(chunk)
82
+ case 'input_audio':
83
+ return { type: chunk.type, input_audio: chunk.inputAudio }
84
+ case 'UNKNOWN':
85
+ // An unknown chunk read from a response. Send what came in.
86
+ return chunk.raw
87
+ }
88
+ }
89
+
90
+ function thinkChunkToWire(chunk: ThinkChunk) {
91
+ return {
92
+ type: 'thinking',
93
+ thinking: chunk.thinking.map((part) => {
94
+ if ('referenceIds' in part) {
95
+ return { type: 'reference', reference_ids: part.referenceIds }
96
+ }
97
+ if ('title' in part) {
98
+ const { tool, title, url, favicon, description } = part
99
+ return {
100
+ type: 'tool_reference',
101
+ tool,
102
+ title,
103
+ url,
104
+ favicon,
105
+ description,
106
+ }
107
+ }
108
+ return { type: 'text', text: part.text }
109
+ }),
110
+ closed: chunk.closed,
111
+ }
112
+ }
113
+
114
+ function contentToWire(
115
+ content: string | Array<ContentChunk> | null | undefined,
116
+ ) {
117
+ return Array.isArray(content) ? content.map(chunkToWire) : content
118
+ }
119
+
120
+ function messageToWire(message: ChatCompletionRequest['messages'][number]) {
121
+ switch (message.role) {
122
+ case 'assistant':
123
+ return {
124
+ role: message.role,
125
+ content: contentToWire(message.content),
126
+ tool_calls: message.toolCalls?.map((call) => ({
127
+ id: call.id ?? 'null',
128
+ type: call.type,
129
+ function: call.function,
130
+ index: call.index ?? 0,
131
+ })),
132
+ prefix: message.prefix ?? false,
133
+ }
134
+ case 'system':
135
+ return { role: message.role, content: contentToWire(message.content) }
136
+ case 'tool':
137
+ return {
138
+ role: message.role,
139
+ content: contentToWire(message.content),
140
+ tool_call_id: message.toolCallId,
141
+ name: message.name,
142
+ }
143
+ case 'user':
144
+ return { role: message.role, content: contentToWire(message.content) }
145
+ }
146
+ }
147
+
148
+ /** The JSON body the SDK sends for `chat.complete`. */
149
+ export function chatCompletionRequestToWire(request: ChatCompletionRequest) {
150
+ const { responseFormat, prediction } = request
151
+ const jsonSchema = responseFormat?.jsonSchema
152
+ return {
153
+ model: request.model,
154
+ temperature: request.temperature,
155
+ top_p: request.topP,
156
+ max_tokens: request.maxTokens,
157
+ stream: request.stream ?? false,
158
+ stop: request.stop,
159
+ random_seed: request.randomSeed,
160
+ metadata: request.metadata,
161
+ messages: request.messages.map(messageToWire),
162
+ response_format: responseFormat && {
163
+ type: responseFormat.type,
164
+ json_schema: jsonSchema && {
165
+ name: jsonSchema.name,
166
+ description: jsonSchema.description,
167
+ schema: jsonSchema.schemaDefinition,
168
+ strict: jsonSchema.strict,
169
+ },
170
+ },
171
+ // ponytail: function tools and tool choice need no renames. Built-in
172
+ // connector tools and `guardrails` go out as given; the adapters never
173
+ // send them. Port their rename tables when an adapter does.
174
+ tools: request.tools,
175
+ tool_choice: request.toolChoice,
176
+ presence_penalty: request.presencePenalty,
177
+ frequency_penalty: request.frequencyPenalty,
178
+ n: request.n,
179
+ prediction: prediction && {
180
+ type: prediction.type ?? 'content',
181
+ content: prediction.content ?? '',
182
+ },
183
+ parallel_tool_calls: request.parallelToolCalls,
184
+ reasoning_effort: request.reasoningEffort,
185
+ prompt_mode: request.promptMode,
186
+ guardrails: request.guardrails,
187
+ safe_prompt: request.safePrompt,
188
+ }
189
+ }
190
+
191
+ /** The JSON body the SDK sends for `embeddings.create`. */
192
+ export function embeddingRequestToWire(request: EmbeddingRequest) {
193
+ return {
194
+ model: request.model,
195
+ metadata: request.metadata,
196
+ input: request.inputs,
197
+ output_dimension: request.outputDimension,
198
+ output_dtype: request.outputDtype,
199
+ encoding_format: request.encodingFormat,
200
+ }
201
+ }
202
+
203
+ // ===========================
204
+ // Inbound: wire snake_case -> SDK camelCase (the SDK `$inboundSchema`s)
205
+ // ===========================
206
+
207
+ /** An assistant message on the wire. The SDK fills `role` and `prefix`. */
208
+ type WireAssistant = Omit<
209
+ AssistantMessage$Outbound,
210
+ 'role' | 'prefix' | 'tool_calls' | 'content'
211
+ > & {
212
+ role?: 'assistant'
213
+ content?: string | Array<WireContentChunk> | null
214
+ prefix?: boolean
215
+ tool_calls?: Array<ToolCall> | null
216
+ }
217
+
218
+ /** A content chunk on the wire. `image_url` keeps the SDK value as is. */
219
+ type WireContentChunk =
220
+ | Exclude<ContentChunk$Outbound, { type: 'image_url' }>
221
+ | { type: 'image_url'; image_url: ImageURLChunk['imageUrl'] }
222
+
223
+ /** Usage on the wire. The SDK fills missing token counts with 0. */
224
+ type WireUsage = Partial<UsageInfo$Outbound>
225
+
226
+ interface WireChoice {
227
+ index: number
228
+ message?: WireAssistant
229
+ messages?: Array<WireAssistant>
230
+ finish_reason: ChatCompletionChoice['finishReason']
231
+ }
232
+
233
+ interface WireChatCompletion {
234
+ id: string
235
+ object: string
236
+ model: string
237
+ usage: WireUsage
238
+ created: number
239
+ choices: Array<WireChoice>
240
+ }
241
+
242
+ interface WireEmbedding {
243
+ id: string
244
+ object: string
245
+ model: string
246
+ usage: WireUsage
247
+ data: Array<EmbeddingResponseData>
248
+ }
249
+
250
+ function usageFromWire(usage: WireUsage): UsageInfo {
251
+ // Unknown keys (`prompt_tokens_details`, `num_cached_tokens`) stay as they
252
+ // are, like the SDK's catchall.
253
+ const {
254
+ prompt_tokens = 0,
255
+ completion_tokens = 0,
256
+ total_tokens = 0,
257
+ prompt_audio_seconds,
258
+ ...rest
259
+ } = usage
260
+ return {
261
+ ...rest,
262
+ promptTokens: prompt_tokens,
263
+ completionTokens: completion_tokens,
264
+ totalTokens: total_tokens,
265
+ ...('prompt_audio_seconds' in usage && {
266
+ promptAudioSeconds: prompt_audio_seconds,
267
+ }),
268
+ }
269
+ }
270
+
271
+ function thinkingFromWire(
272
+ part: Thinking$Outbound,
273
+ ): ThinkChunk['thinking'][number] {
274
+ switch (part.type) {
275
+ case 'reference':
276
+ return { type: part.type, referenceIds: part.reference_ids }
277
+ case 'tool_reference':
278
+ return part
279
+ case 'text':
280
+ return { type: part.type, text: part.text }
281
+ }
282
+ }
283
+
284
+ function chunkFromWire(chunk: WireContentChunk): ContentChunk {
285
+ switch (chunk.type) {
286
+ case 'image_url':
287
+ return { type: chunk.type, imageUrl: chunk.image_url }
288
+ case 'document_url':
289
+ return {
290
+ type: chunk.type,
291
+ documentUrl: chunk.document_url,
292
+ documentName: chunk.document_name,
293
+ }
294
+ case 'text':
295
+ return { type: chunk.type, text: chunk.text }
296
+ case 'reference':
297
+ return { type: chunk.type, referenceIds: chunk.reference_ids }
298
+ case 'file':
299
+ return { type: chunk.type, fileId: chunk.file_id }
300
+ case 'thinking':
301
+ return {
302
+ type: chunk.type,
303
+ thinking: chunk.thinking.map(thinkingFromWire),
304
+ closed: chunk.closed,
305
+ }
306
+ case 'input_audio':
307
+ return { type: chunk.type, inputAudio: chunk.input_audio }
308
+ default:
309
+ return { raw: chunk, type: 'UNKNOWN', isUnknown: true }
310
+ }
311
+ }
312
+
313
+ function toolCallFromWire(call: ToolCall): ToolCall {
314
+ return {
315
+ id: call.id ?? 'null',
316
+ type: call.type,
317
+ function: { name: call.function.name, arguments: call.function.arguments },
318
+ index: call.index ?? 0,
319
+ }
320
+ }
321
+
322
+ function assistantFromWire(message: WireAssistant): AssistantMessage {
323
+ const { content, tool_calls: toolCalls } = message
324
+ return {
325
+ role: message.role ?? 'assistant',
326
+ content: Array.isArray(content) ? content.map(chunkFromWire) : content,
327
+ toolCalls: toolCalls && toolCalls.map(toolCallFromWire),
328
+ prefix: message.prefix ?? false,
329
+ }
330
+ }
331
+
332
+ /** The SDK `ChatCompletionResponse` for a wire response. */
333
+ export function chatCompletionResponseFromWire(
334
+ wire: WireChatCompletion,
335
+ ): ChatCompletionResponse {
336
+ return {
337
+ id: wire.id,
338
+ object: wire.object,
339
+ model: wire.model,
340
+ usage: usageFromWire(wire.usage),
341
+ created: wire.created,
342
+ choices: wire.choices.map((choice) => ({
343
+ index: choice.index,
344
+ message: choice.message && assistantFromWire(choice.message),
345
+ messages: choice.messages?.map(assistantFromWire),
346
+ finishReason: choice.finish_reason,
347
+ })),
348
+ }
349
+ }
350
+
351
+ /** The SDK `EmbeddingResponse` for a wire response. */
352
+ export function embeddingResponseFromWire(
353
+ wire: WireEmbedding,
354
+ ): EmbeddingResponse {
355
+ return {
356
+ id: wire.id,
357
+ object: wire.object,
358
+ model: wire.model,
359
+ usage: usageFromWire(wire.usage),
360
+ data: wire.data.map((item) => ({
361
+ object: item.object,
362
+ embedding: item.embedding,
363
+ index: item.index,
364
+ })),
365
+ }
366
+ }
367
+
368
+ // ===========================
369
+ // Errors
370
+ // ===========================
371
+
372
+ /**
373
+ * The SDK's `HTTPValidationError` (422), with the same name, message, and
374
+ * fields. The SDK class module loads zod, so this copy stands in for it.
375
+ * It extends the real `MistralError`.
376
+ */
377
+ export class HTTPValidationError extends MistralError {
378
+ detail?: Array<ValidationError>
379
+ data$: {
380
+ detail?: Array<ValidationError>
381
+ request$: Request
382
+ response$: Response
383
+ body$: string
384
+ }
385
+
386
+ constructor(
387
+ err: HTTPValidationError['data$'],
388
+ httpMeta: { request: Request; response: Response; body: string },
389
+ ) {
390
+ // The SDK schema strips `message`, so the SDK always builds this text.
391
+ super(`API error occurred: ${JSON.stringify(err)}`, httpMeta)
392
+ this.data$ = err
393
+ if (err.detail != null) this.detail = err.detail
394
+ this.name = 'HTTPValidationError'
395
+ }
396
+ }
397
+
398
+ const hasCode = (err: object, test: (code: unknown) => boolean) =>
399
+ 'code' in err && test(err.code)
400
+
401
+ const isEconn = (suffix: string) => (code: unknown) =>
402
+ typeof code === 'string' && code.toLowerCase() === `econn${suffix}`
403
+
404
+ /** The SDK `_do` error mapping for a failed fetch. */
405
+ function toClientError(err: unknown) {
406
+ if (typeof err !== 'object' || err === null) {
407
+ return new UnexpectedClientError('Unexpected HTTP client error', {
408
+ cause: err,
409
+ })
410
+ }
411
+ const name = 'name' in err ? err.name : undefined
412
+ const isAbort =
413
+ name === 'AbortError' ||
414
+ hasCode(err, (code) => code === 20) ||
415
+ hasCode(err, isEconn('aborted'))
416
+ if (isAbort) {
417
+ return new RequestAbortedError('Request aborted by client', { cause: err })
418
+ }
419
+ const isTimeout =
420
+ name === 'TimeoutError' || hasCode(err, (code) => code === 23)
421
+ if (isTimeout) {
422
+ return new RequestTimeoutError('Request timed out', { cause: err })
423
+ }
424
+ const message = err instanceof TypeError ? err.message.toLowerCase() : ''
425
+ const isConnection =
426
+ message.startsWith('failed to fetch') ||
427
+ message.startsWith('fetch failed') ||
428
+ name === 'ConnectionError' ||
429
+ hasCode(err, isEconn('reset'))
430
+ if (isConnection) {
431
+ return new ConnectionError('Unable to make request', { cause: err })
432
+ }
433
+ return new UnexpectedClientError('Unexpected HTTP client error', {
434
+ cause: err,
435
+ })
436
+ }
437
+
438
+ const isJSONResponse = (response: Response) =>
439
+ (response.headers.get('content-type')?.trim() || '')
440
+ .toLowerCase()
441
+ .split(/\s*;\s*/)[0] === 'application/json'
442
+
443
+ const isStatusFamily = (response: Response, family: string) =>
444
+ String(response.status).startsWith(family)
445
+
446
+ // ===========================
447
+ // Client
448
+ // ===========================
449
+
450
+ /**
451
+ * Creates the Mistral client the adapters use. It sends the same requests as
452
+ * the SDK `Mistral` client (no retries, 30 s default timeout) with `fetch`.
453
+ * A `fetcher` replaces `fetch`; it gets `(url, init)`.
454
+ *
455
+ * @throws The SDK error classes: `SDKError` for a 4xx/5xx or an unexpected
456
+ * response, `HTTPValidationError` for a 422 JSON body, and the HTTP client
457
+ * errors (`ConnectionError`, `RequestTimeoutError`, `RequestAbortedError`,
458
+ * `UnexpectedClientError`) when the request fails.
459
+ */
460
+ export function createMistralClient(
461
+ config: MistralClientConfig,
462
+ fetcher?: Fetcher,
463
+ ): MistralClient {
464
+ const { apiKey, defaultHeaders, getAccessToken, resolveRequestUrl } = config
465
+ const baseURL = new URL(
466
+ config.baseURL || config.serverURL || 'https://api.mistral.ai',
467
+ )
468
+ baseURL.pathname = `${baseURL.pathname.replace(/\/+$/, '')}/`
469
+ const timeoutMs = config.timeoutMs || 30000
470
+
471
+ async function post(
472
+ path: string,
473
+ body: unknown,
474
+ requestOptions?: MistralRequestOptions,
475
+ ) {
476
+ const url = new URL(path, baseURL)
477
+ url.hash = ''
478
+ const headers = new Headers({
479
+ 'Content-Type': 'application/json',
480
+ Accept: 'application/json',
481
+ })
482
+ if (apiKey) {
483
+ const isBearer = apiKey.slice(0, 7).toLowerCase() === 'bearer '
484
+ headers.set('Authorization', isBearer ? apiKey : `Bearer ${apiKey}`)
485
+ }
486
+ const init: RequestInit = {
487
+ method: 'POST',
488
+ headers,
489
+ body: JSON.stringify(body),
490
+ signal:
491
+ requestOptions?.signal ??
492
+ requestOptions?.fetchOptions?.signal ??
493
+ (timeoutMs > 0 ? AbortSignal.timeout(timeoutMs) : undefined),
494
+ }
495
+ const send =
496
+ fetcher ??
497
+ ((input: string, options: RequestInit) => fetch(input, options))
498
+
499
+ let requestURL = url.href
500
+ let response: Response
501
+ try {
502
+ // The SDK runs these in its request hook, so their errors map the same.
503
+ if (resolveRequestUrl !== undefined) requestURL = resolveRequestUrl(false)
504
+ for (const [key, value] of Object.entries(defaultHeaders ?? {})) {
505
+ headers.set(key, value)
506
+ }
507
+ if (getAccessToken !== undefined) {
508
+ headers.set('Authorization', `Bearer ${await getAccessToken()}`)
509
+ }
510
+ response = await send(requestURL, init)
511
+ } catch (err) {
512
+ throw toClientError(err)
513
+ }
514
+
515
+ const isJSON = isJSONResponse(response)
516
+ if (response.status === 200 && isJSON) {
517
+ const wire = JSON.parse(await response.text())
518
+ const deprecation = response.headers.get('x-model-deprecation-timestamp')
519
+ if (deprecation !== null) {
520
+ console.warn(
521
+ `WARNING: The model ${wire.model} is deprecated and will be removed on ${deprecation}. Please refer to https://docs.mistral.ai/getting-started/models/#api-versioning for more information.`,
522
+ )
523
+ }
524
+ return wire
525
+ }
526
+
527
+ const request = new Request(requestURL, {
528
+ method: 'POST',
529
+ headers,
530
+ body: init.body,
531
+ })
532
+ const isAPIError =
533
+ isStatusFamily(response, '4') || isStatusFamily(response, '5')
534
+ if (response.status === 422 && isJSON) {
535
+ const text = await response.text()
536
+ const raw: { detail?: Array<ValidationError> } = JSON.parse(text)
537
+ const detail = raw.detail?.map(({ loc, msg, type, input, ctx }) => ({
538
+ loc,
539
+ msg,
540
+ type,
541
+ input,
542
+ ...(ctx !== undefined && { ctx: {} }),
543
+ }))
544
+ throw new HTTPValidationError(
545
+ {
546
+ ...(detail !== undefined && { detail }),
547
+ request$: request,
548
+ response$: response,
549
+ body$: text,
550
+ },
551
+ { request, response, body: text },
552
+ )
553
+ }
554
+ if (isAPIError) {
555
+ const text = await response.text()
556
+ throw new SDKError('API error occurred', {
557
+ request,
558
+ response,
559
+ body: text,
560
+ })
561
+ }
562
+ const text = await response.text().catch(() => '')
563
+ throw new SDKError('Unexpected Status or Content-Type', {
564
+ request,
565
+ response,
566
+ body: text,
567
+ })
568
+ }
569
+
570
+ return {
571
+ chat: {
572
+ complete: async (request, options) =>
573
+ chatCompletionResponseFromWire(
574
+ await post(
575
+ 'v1/chat/completions',
576
+ chatCompletionRequestToWire(request),
577
+ options,
578
+ ),
579
+ ),
580
+ },
581
+ embeddings: {
582
+ create: async (request, options) =>
583
+ embeddingResponseFromWire(
584
+ await post('v1/embeddings', embeddingRequestToWire(request), options),
585
+ ),
586
+ },
587
+ }
588
+ }
@@ -1,5 +1,3 @@
1
- import { HTTPClient, Mistral } from '@mistralai/mistralai'
2
-
3
1
  export interface MistralClientConfig {
4
2
  /** Mistral API key. */
5
3
  apiKey: string
@@ -36,71 +34,6 @@ export interface MistralClientConfig {
36
34
  requestModel?: string
37
35
  }
38
36
 
39
- /**
40
- * Creates a Mistral SDK client instance. A `fetcher` replaces the SDK's fetch.
41
- */
42
- export function createMistralClient(
43
- config: MistralClientConfig,
44
- fetcher?: typeof fetch,
45
- ): Mistral {
46
- const {
47
- apiKey,
48
- baseURL,
49
- timeoutMs,
50
- defaultHeaders,
51
- getAccessToken,
52
- resolveRequestUrl,
53
- } = config
54
- const serverURL = baseURL ?? config.serverURL
55
-
56
- const needsHook =
57
- (defaultHeaders !== undefined && Object.keys(defaultHeaders).length > 0) ||
58
- getAccessToken !== undefined ||
59
- resolveRequestUrl !== undefined
60
-
61
- let httpClient: HTTPClient | undefined
62
- if (needsHook || fetcher) {
63
- httpClient = new HTTPClient({
64
- // The SDK sends a Request. Send a URL and an init, as a plain fetch
65
- // call does, so a wrapper that reads `init.headers` keeps them.
66
- fetcher:
67
- fetcher &&
68
- (async (input) => {
69
- const request = new Request(input)
70
- return fetcher(request.url, {
71
- method: request.method,
72
- headers: request.headers,
73
- body: request.body && (await request.arrayBuffer()),
74
- signal: request.signal,
75
- })
76
- }),
77
- })
78
- }
79
- if (httpClient && needsHook) {
80
- httpClient.addHook('beforeRequest', async (req) => {
81
- const nextUrl =
82
- resolveRequestUrl === undefined ? req.url : resolveRequestUrl(false)
83
- const next = new Request(nextUrl, req)
84
- if (defaultHeaders) {
85
- for (const [key, value] of Object.entries(defaultHeaders)) {
86
- next.headers.set(key, value)
87
- }
88
- }
89
- if (getAccessToken !== undefined) {
90
- next.headers.set('Authorization', `Bearer ${await getAccessToken()}`)
91
- }
92
- return next
93
- })
94
- }
95
-
96
- return new Mistral({
97
- apiKey,
98
- ...(serverURL !== undefined ? { serverURL } : {}),
99
- ...(timeoutMs !== undefined ? { timeoutMs } : {}),
100
- ...(httpClient !== undefined ? { httpClient } : {}),
101
- })
102
- }
103
-
104
37
  /**
105
38
  * Gets Mistral API key from environment variables.
106
39
  * @throws Error if MISTRAL_API_KEY is not found