@tanstack/ai-mistral 0.6.18 → 0.7.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.
@@ -1,6 +1,6 @@
1
1
  import { BaseTextAdapter, StructuredOutputOptions, StructuredOutputResult } from '@tanstack/ai/adapters';
2
2
  import { Modality, AdapterYieldChunk, TextOptions } from '@tanstack/ai';
3
- import { MISTRAL_CHAT_MODELS, MistralChatModelProviderOptionsByName, MistralModelInputModalitiesByName, MistralTextAdapterModel } from '../model-meta.js';
3
+ import { MISTRAL_CHAT_MODELS, MistralChatModelProviderOptionsByName, MistralModelInputModalitiesByName, MistralModelReasoningByName, MistralTextAdapterModel } from '../model-meta.js';
4
4
  import { ExternalTextProviderOptions } from '../text/text-provider-options.js';
5
5
  import { MistralMessageMetadataByModality } from '../message-types.js';
6
6
  import { MistralClientConfig } from '../utils/client.js';
@@ -13,14 +13,18 @@ export type MistralTextConfig = MistralClientConfig;
13
13
  */
14
14
  export type MistralTextProviderOptions = ExternalTextProviderOptions;
15
15
  type ResolveProviderOptions<TModel extends string> = TModel extends keyof MistralChatModelProviderOptionsByName ? MistralChatModelProviderOptionsByName[TModel] : MistralTextProviderOptions;
16
+ /** The reasoning levels of a model, for `chat({ reasoning })`. `never`: none. */
17
+ type ResolveReasoning<TModel extends string> = TModel extends keyof MistralModelReasoningByName ? MistralModelReasoningByName[TModel] : never;
16
18
  type ResolveInputModalities<TModel extends string> = TModel extends keyof MistralModelInputModalitiesByName ? MistralModelInputModalitiesByName[TModel] : readonly ['text'];
17
19
  /**
18
20
  * Mistral Text (Chat) Adapter.
19
21
  *
20
22
  * Tree-shakeable adapter for Mistral chat/text completion functionality.
21
23
  */
22
- export declare class MistralTextAdapter<TModel extends MistralTextAdapterModel, TProviderOptions extends Record<string, any> = ResolveProviderOptions<TModel>, TInputModalities extends ReadonlyArray<Modality> = ResolveInputModalities<TModel>> extends BaseTextAdapter<TModel, TProviderOptions, TInputModalities, MistralMessageMetadataByModality> {
24
+ export declare class MistralTextAdapter<TModel extends MistralTextAdapterModel, TProviderOptions extends Record<string, any> = ResolveProviderOptions<TModel>, TInputModalities extends ReadonlyArray<Modality> = ResolveInputModalities<TModel>> extends BaseTextAdapter<TModel, TProviderOptions, TInputModalities, MistralMessageMetadataByModality, ReadonlyArray<string>, unknown, never, ResolveReasoning<TModel>> {
23
25
  readonly name: "mistral";
26
+ readonly api: "mistral-conversations";
27
+ readonly provider: string;
24
28
  readonly inputModalities: readonly ("text" | "image" | "audio" | "document" | "video")[] | undefined;
25
29
  private readonly client;
26
30
  private readonly rawConfig;
@@ -62,6 +66,8 @@ export declare class MistralTextAdapter<TModel extends MistralTextAdapterModel,
62
66
  * Converts a TanStack AI ModelMessage to a Mistral ChatCompletionMessageParam.
63
67
  */
64
68
  private convertMessageToMistral;
69
+ /** Readable thinking and text, in the stored block order. */
70
+ private assistantContent;
65
71
  /**
66
72
  * Converts a ContentPart to a Mistral content part. Returns undefined for
67
73
  * unsupported part types.
@@ -2,10 +2,11 @@ import { makeMistralStructuredOutputCompatibleWithMap } from "../utils/schema-co
2
2
  import { convertToolsToProviderFormat } from "../tools/tool-converter.js";
3
3
  import { createMistralClient, generateId, getMistralApiKeyFromEnv } from "../utils/client.js";
4
4
  import { createToolInputNormalizer } from "../utils/tool-input-normalizer.js";
5
- import { MISTRAL_MODEL_INPUT_MODALITIES } from "../model-meta.js";
5
+ import { MISTRAL_MODEL_INPUT_MODALITIES, MISTRAL_MODEL_REASONING } from "../model-meta.js";
6
6
  import { isFileSource, unsupportedFileSourceError } from "@tanstack/ai";
7
7
  import { BaseTextAdapter } from "@tanstack/ai/adapters";
8
8
  import { undoNullWidening } from "@tanstack/ai-utils";
9
+ import { hashToolCallId, orderedAssistantBlocks, resolveReasoning, transformMessagesForReplay } from "@tanstack/ai/adapter-internals";
9
10
  //#region src/adapters/text.ts
10
11
  /** Cast an event object to StreamChunk. Adapters construct events with string
11
12
  * literal types which are structurally compatible with the EventType enum. */
@@ -26,12 +27,86 @@ function parseToolCallInput(toolCall, normalizeToolInput) {
26
27
  }
27
28
  }
28
29
  /**
30
+ * The Mistral reasoning fields for `chat({ reasoning })`, by pi's rule:
31
+ * - a model with effort levels (Mistral Small, Mistral Medium):
32
+ * `reasoningEffort` with the level's value, `none` for `off`.
33
+ * - Magistral: `promptMode: 'reasoning'`, and nothing for `off`.
34
+ */
35
+ function mistralReasoning(model, request, reasoning) {
36
+ const resolved = resolveReasoning(request, reasoning);
37
+ if (!resolved) return {};
38
+ if (model.startsWith("magistral")) return resolved.level === "off" ? {} : { promptMode: "reasoning" };
39
+ return { reasoningEffort: resolved.value };
40
+ }
41
+ /** Maps `chat({ toolChoice })` to the Mistral `toolChoice`. */
42
+ function toMistralToolChoice(choice) {
43
+ if (typeof choice === "string") return choice;
44
+ return {
45
+ type: "function",
46
+ function: { name: choice.name }
47
+ };
48
+ }
49
+ /**
50
+ * The Mistral wire field for `chat({ promptCache })`. Mistral caches without
51
+ * a flag, so only the key goes out. There is no key for `'none'`. Send the
52
+ * key only to `api.mistral.ai`. The SDK request schema drops unknown fields,
53
+ * so structured output adds the key after that schema runs.
54
+ */
55
+ function mistralPromptCacheKey(promptCache) {
56
+ if (!promptCache?.key || promptCache.retention === "none") return void 0;
57
+ return promptCache.key;
58
+ }
59
+ function mistralOwnApi(config, provider) {
60
+ if (provider === "google-vertex") return false;
61
+ const serverURL = config.baseURL ?? config.serverURL ?? "https://api.mistral.ai";
62
+ try {
63
+ return new URL(serverURL).hostname === "api.mistral.ai";
64
+ } catch {
65
+ return false;
66
+ }
67
+ }
68
+ /** Add `prompt_cache_key` to a JSON body the SDK has already serialized. */
69
+ function withPromptCacheKey(next, key) {
70
+ return async (input, init) => {
71
+ const body = init?.body;
72
+ const text = typeof body === "string" ? body : body instanceof ArrayBuffer ? new TextDecoder().decode(body) : void 0;
73
+ if (text === void 0) return next(input, init);
74
+ let parsed;
75
+ try {
76
+ parsed = JSON.parse(text);
77
+ } catch {
78
+ return next(input, init);
79
+ }
80
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return next(input, init);
81
+ return next(input, {
82
+ ...init,
83
+ body: JSON.stringify({
84
+ ...parsed,
85
+ prompt_cache_key: key
86
+ })
87
+ });
88
+ };
89
+ }
90
+ /**
91
+ * `promptTokensDetails.cachedTokens` when Mistral read prompt tokens from the
92
+ * cache. Mistral sends the count in `prompt_tokens_details.cached_tokens` or
93
+ * in `num_cached_tokens`. The SDK keeps these raw keys on `usage` with no
94
+ * types, so the values are checked here.
95
+ */
96
+ function mistralCachedTokens(usage) {
97
+ const details = usage.prompt_tokens_details;
98
+ const cachedTokens = typeof details === "object" && details !== null && "cached_tokens" in details ? details.cached_tokens : usage.num_cached_tokens;
99
+ return typeof cachedTokens === "number" && cachedTokens > 0 ? { promptTokensDetails: { cachedTokens } } : {};
100
+ }
101
+ /**
29
102
  * Mistral Text (Chat) Adapter.
30
103
  *
31
104
  * Tree-shakeable adapter for Mistral chat/text completion functionality.
32
105
  */
33
106
  var MistralTextAdapter = class extends BaseTextAdapter {
34
107
  name = "mistral";
108
+ api = "mistral-conversations";
109
+ provider;
35
110
  inputModalities = MISTRAL_MODEL_INPUT_MODALITIES[this.model];
36
111
  client;
37
112
  rawConfig;
@@ -39,6 +114,7 @@ var MistralTextAdapter = class extends BaseTextAdapter {
39
114
  super(config, model);
40
115
  this.client = createMistralClient(config);
41
116
  this.rawConfig = config;
117
+ this.provider = config.getAccessToken && config.resolveRequestUrl ? "google-vertex" : this.name;
42
118
  }
43
119
  async *chatStream(options) {
44
120
  const requestParams = this.mapTextOptionsToMistral(options);
@@ -51,8 +127,26 @@ var MistralTextAdapter = class extends BaseTextAdapter {
51
127
  hasEmittedRunStarted: false
52
128
  };
53
129
  try {
54
- const stream = this.fetchRawMistralStream(requestParams, this.rawConfig, options.wrapFetch);
55
- yield* this.processMistralStreamChunks(stream, options, aguiState);
130
+ const cacheKey = mistralOwnApi(this.rawConfig, this.provider) ? mistralPromptCacheKey(options.promptCache) : void 0;
131
+ const body = {
132
+ ...this.toWireBody(requestParams),
133
+ ...cacheKey !== void 0 && { prompt_cache_key: cacheKey }
134
+ };
135
+ const stream = this.fetchRawMistralStream(body, this.rawConfig, options.wrapFetch);
136
+ for await (const chunk of this.processMistralStreamChunks(stream, options, aguiState)) yield chunk.type === "RUN_STARTED" || chunk.type === "RUN_ERROR" ? {
137
+ ...chunk,
138
+ metadata: {
139
+ ...chunk.metadata,
140
+ tanstack: {
141
+ ...chunk.metadata?.tanstack,
142
+ source: {
143
+ provider: this.provider,
144
+ api: this.api,
145
+ model: options.model
146
+ }
147
+ }
148
+ }
149
+ } : chunk;
56
150
  } catch (error) {
57
151
  const err = error;
58
152
  if (!aguiState.hasEmittedRunStarted) {
@@ -61,12 +155,22 @@ var MistralTextAdapter = class extends BaseTextAdapter {
61
155
  type: "RUN_STARTED",
62
156
  runId: aguiState.runId,
63
157
  threadId: aguiState.threadId,
158
+ metadata: { tanstack: { source: {
159
+ provider: this.provider,
160
+ api: this.api,
161
+ model: options.model
162
+ } } },
64
163
  model: options.model,
65
164
  timestamp
66
165
  });
67
166
  }
68
167
  yield asChunk({
69
168
  type: "RUN_ERROR",
169
+ metadata: { tanstack: { source: {
170
+ provider: this.provider,
171
+ api: this.api,
172
+ model: options.model
173
+ } } },
70
174
  runId: aguiState.runId,
71
175
  model: options.model,
72
176
  timestamp,
@@ -87,7 +191,9 @@ var MistralTextAdapter = class extends BaseTextAdapter {
87
191
  const { chatOptions, outputSchema } = options;
88
192
  const { stream: _stream, ...nonStreamParams } = this.mapTextOptionsToMistral(chatOptions);
89
193
  const { schema: jsonSchema, nullWideningMap, strict } = makeMistralStructuredOutputCompatibleWithMap(outputSchema, outputSchema.required || []);
90
- const response = await (chatOptions.wrapFetch ? createMistralClient(this.rawConfig, chatOptions.wrapFetch(fetch)) : this.client).chat.complete({
194
+ const cacheKey = mistralOwnApi(this.rawConfig, this.provider) ? mistralPromptCacheKey(chatOptions.promptCache) : void 0;
195
+ const next = chatOptions.wrapFetch ? chatOptions.wrapFetch(fetch) : fetch;
196
+ const response = await (cacheKey ? createMistralClient(this.rawConfig, withPromptCacheKey(next, cacheKey)) : chatOptions.wrapFetch ? createMistralClient(this.rawConfig, next) : this.client).chat.complete({
91
197
  ...nonStreamParams,
92
198
  responseFormat: {
93
199
  type: "json_schema",
@@ -110,11 +216,14 @@ var MistralTextAdapter = class extends BaseTextAdapter {
110
216
  const usage = response.usage;
111
217
  return {
112
218
  data: undoNullWidening(parsed, nullWideningMap),
219
+ ...response.id && { responseId: response.id },
220
+ ...response.model && { model: response.model },
113
221
  rawText: textContent,
114
222
  ...usage && { usage: {
115
223
  promptTokens: usage.promptTokens ?? 0,
116
224
  completionTokens: usage.completionTokens ?? 0,
117
- totalTokens: usage.totalTokens ?? 0
225
+ totalTokens: usage.totalTokens ?? 0,
226
+ ...mistralCachedTokens(usage)
118
227
  } }
119
228
  };
120
229
  }
@@ -129,13 +238,15 @@ var MistralTextAdapter = class extends BaseTextAdapter {
129
238
  let hasEmittedToolCall = false;
130
239
  let hasEmittedRunFinished = false;
131
240
  let lastChunkModel = options.model;
241
+ let responseId;
132
242
  const normalizeToolInput = createToolInputNormalizer(options.tools);
133
243
  let reasoningMessageId = null;
134
244
  let hasClosedReasoning = false;
135
245
  const toolCallsInProgress = /* @__PURE__ */ new Map();
136
246
  try {
137
247
  for await (const chunk of stream) {
138
- lastChunkModel = chunk.model || options.model;
248
+ lastChunkModel = chunk.model || lastChunkModel;
249
+ if (chunk.id) responseId = chunk.id;
139
250
  const choice = chunk.choices?.[0];
140
251
  if (!choice) continue;
141
252
  const chunkModel = chunk.model || options.model;
@@ -273,6 +384,7 @@ var MistralTextAdapter = class extends BaseTextAdapter {
273
384
  toolName: toolCall.name,
274
385
  model: chunkModel,
275
386
  timestamp,
387
+ args: toolCall.arguments,
276
388
  input: parsedInput
277
389
  });
278
390
  }
@@ -305,6 +417,7 @@ var MistralTextAdapter = class extends BaseTextAdapter {
305
417
  hasEmittedRunFinished = true;
306
418
  yield asChunk({
307
419
  type: "RUN_FINISHED",
420
+ ...responseId && { responseId },
308
421
  runId: aguiState.runId,
309
422
  threadId: aguiState.threadId,
310
423
  model: chunkModel,
@@ -312,7 +425,8 @@ var MistralTextAdapter = class extends BaseTextAdapter {
312
425
  usage: usage ? {
313
426
  promptTokens: usage.prompt_tokens || 0,
314
427
  completionTokens: usage.completion_tokens || 0,
315
- totalTokens: usage.total_tokens || 0
428
+ totalTokens: usage.total_tokens || 0,
429
+ ...mistralCachedTokens(usage)
316
430
  } : void 0,
317
431
  finishReason: computedFinishReason
318
432
  });
@@ -335,6 +449,7 @@ var MistralTextAdapter = class extends BaseTextAdapter {
335
449
  });
336
450
  }
337
451
  for (const [, toolCall] of toolCallsInProgress) if (toolCall.started && !toolCall.ended) {
452
+ const parsedInput = parseToolCallInput(toolCall, normalizeToolInput);
338
453
  toolCall.ended = true;
339
454
  hasEmittedToolCall = true;
340
455
  yield asChunk({
@@ -344,7 +459,8 @@ var MistralTextAdapter = class extends BaseTextAdapter {
344
459
  toolName: toolCall.name,
345
460
  model: lastChunkModel,
346
461
  timestamp,
347
- input: parseToolCallInput(toolCall, normalizeToolInput)
462
+ args: toolCall.arguments,
463
+ input: parsedInput
348
464
  });
349
465
  }
350
466
  if (hasEmittedTextMessageStart && !hasEmittedTextMessageEnd) {
@@ -359,6 +475,7 @@ var MistralTextAdapter = class extends BaseTextAdapter {
359
475
  hasEmittedRunFinished = true;
360
476
  yield asChunk({
361
477
  type: "RUN_FINISHED",
478
+ ...responseId && { responseId },
362
479
  runId: aguiState.runId,
363
480
  threadId: aguiState.threadId,
364
481
  model: lastChunkModel,
@@ -394,11 +511,11 @@ var MistralTextAdapter = class extends BaseTextAdapter {
394
511
  }
395
512
  for (const [, toolCall] of toolCallsInProgress) if (toolCall.started && !toolCall.ended) {
396
513
  toolCall.ended = true;
397
- let partialInput = {};
514
+ let partialInput;
398
515
  try {
399
516
  partialInput = toolCall.arguments ? normalizeToolInput(toolCall.name, JSON.parse(toolCall.arguments)) : {};
400
517
  } catch {
401
- partialInput = {};
518
+ partialInput = void 0;
402
519
  }
403
520
  yield asChunk({
404
521
  type: "TOOL_CALL_END",
@@ -407,7 +524,8 @@ var MistralTextAdapter = class extends BaseTextAdapter {
407
524
  toolName: toolCall.name,
408
525
  model: lastChunkModel,
409
526
  timestamp,
410
- input: partialInput
527
+ args: toolCall.arguments,
528
+ ...partialInput !== void 0 && { input: partialInput }
411
529
  });
412
530
  }
413
531
  throw error;
@@ -418,10 +536,9 @@ var MistralTextAdapter = class extends BaseTextAdapter {
418
536
  * parses the SSE stream manually, bypassing the SDK's Zod validation which
419
537
  * rejects streaming tool call chunks that omit `name` in argument deltas.
420
538
  */
421
- async *fetchRawMistralStream(params, config, wrapFetch) {
539
+ async *fetchRawMistralStream(body, config, wrapFetch) {
422
540
  const serverURL = (config.baseURL ?? config.serverURL ?? "https://api.mistral.ai").replace(/\/+$/, "").replace(/\/v1$/, "");
423
541
  const url = config.resolveRequestUrl?.(true) ?? `${serverURL}/v1/chat/completions`;
424
- const body = this.toWireBody(params);
425
542
  const headers = {
426
543
  "Content-Type": "application/json",
427
544
  Authorization: `Bearer ${config.getAccessToken === void 0 ? config.apiKey : await config.getAccessToken()}`,
@@ -480,7 +597,7 @@ var MistralTextAdapter = class extends BaseTextAdapter {
480
597
  * snake_case wire body, including converting messages.
481
598
  */
482
599
  toWireBody(params) {
483
- const { messages, maxTokens, topP, randomSeed, responseFormat, toolChoice, parallelToolCalls, frequencyPenalty, presencePenalty, safePrompt, stream: _stream, ...rest } = params;
600
+ const { messages, maxTokens, topP, randomSeed, responseFormat, toolChoice, parallelToolCalls, frequencyPenalty, presencePenalty, safePrompt, reasoningEffort, promptMode, stream: _stream, ...rest } = params;
484
601
  return {
485
602
  ...rest,
486
603
  messages: messages.map(messageToWire),
@@ -494,7 +611,9 @@ var MistralTextAdapter = class extends BaseTextAdapter {
494
611
  ...parallelToolCalls != null && { parallel_tool_calls: parallelToolCalls },
495
612
  ...frequencyPenalty != null && { frequency_penalty: frequencyPenalty },
496
613
  ...presencePenalty != null && { presence_penalty: presencePenalty },
497
- ...safePrompt != null && { safe_prompt: safePrompt }
614
+ ...safePrompt != null && { safe_prompt: safePrompt },
615
+ ...reasoningEffort != null && { reasoning_effort: reasoningEffort },
616
+ ...promptMode != null && { prompt_mode: promptMode }
498
617
  };
499
618
  }
500
619
  /**
@@ -531,13 +650,24 @@ var MistralTextAdapter = class extends BaseTextAdapter {
531
650
  mapTextOptionsToMistral(options) {
532
651
  const modelOptions = options.modelOptions;
533
652
  const tools = options.tools ? convertToolsToProviderFormat(options.tools) : void 0;
653
+ const replay = transformMessagesForReplay(options.messages, {
654
+ provider: this.provider,
655
+ api: this.api,
656
+ model: options.model
657
+ }, (id, { attempt }) => {
658
+ const normalized = id.replace(/[^a-zA-Z0-9]/g, "");
659
+ if (attempt === 0 && normalized.length === 9) return normalized;
660
+ const seed = normalized || id;
661
+ return hashToolCallId(attempt === 0 ? seed : `${seed}:${attempt}`).replace(/[^a-zA-Z0-9]/g, "").slice(0, 9);
662
+ });
534
663
  const messages = [];
535
664
  if (options.systemPrompts && options.systemPrompts.length > 0) messages.push({
536
665
  role: "system",
537
666
  content: options.systemPrompts.join("\n")
538
667
  });
539
- for (const message of options.messages) messages.push(this.convertMessageToMistral(message));
668
+ for (const message of replay.messages) messages.push(this.convertMessageToMistral(message));
540
669
  return {
670
+ ...tools?.length && options.toolChoice !== void 0 ? { toolChoice: toMistralToolChoice(options.toolChoice) } : void 0,
541
671
  model: this.rawConfig.requestModel ?? options.model,
542
672
  messages,
543
673
  temperature: modelOptions?.temperature ?? void 0,
@@ -545,6 +675,7 @@ var MistralTextAdapter = class extends BaseTextAdapter {
545
675
  topP: modelOptions?.top_p ?? void 0,
546
676
  tools,
547
677
  stream: true,
678
+ ...mistralReasoning(options.model, options.reasoning, MISTRAL_MODEL_REASONING[options.model]),
548
679
  ...modelOptions && {
549
680
  ...modelOptions.stop != null && { stop: modelOptions.stop },
550
681
  ...modelOptions.random_seed != null && { randomSeed: modelOptions.random_seed },
@@ -582,7 +713,7 @@ var MistralTextAdapter = class extends BaseTextAdapter {
582
713
  }));
583
714
  return {
584
715
  role: "assistant",
585
- content: this.extractTextContent(message.content),
716
+ content: this.assistantContent(message),
586
717
  ...toolCalls && toolCalls.length > 0 ? { toolCalls } : {}
587
718
  };
588
719
  }
@@ -597,6 +728,30 @@ var MistralTextAdapter = class extends BaseTextAdapter {
597
728
  content: parts.length > 0 ? parts : ""
598
729
  };
599
730
  }
731
+ /** Readable thinking and text, in the stored block order. */
732
+ assistantContent(message) {
733
+ if (!message.thinking?.length) return this.extractTextContent(message.content);
734
+ const blocks = orderedAssistantBlocks(message) ?? [...message.thinking.map((thinking) => ({
735
+ type: "thinking",
736
+ thinking
737
+ })), {
738
+ type: "text",
739
+ text: this.extractTextContent(message.content)
740
+ }];
741
+ const content = [];
742
+ for (const block of blocks) if (block.type === "text") content.push({
743
+ type: "text",
744
+ text: block.text
745
+ });
746
+ else if (block.type === "thinking" && !block.thinking.redacted && block.thinking.content.trim()) content.push({
747
+ type: "thinking",
748
+ thinking: [{
749
+ type: "text",
750
+ text: block.thinking.content
751
+ }]
752
+ });
753
+ return content;
754
+ }
600
755
  /**
601
756
  * Converts a ContentPart to a Mistral content part. Returns undefined for
602
757
  * unsupported part types.