@tanstack/ai-mistral 0.6.18 → 0.7.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.
@@ -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;
@@ -305,6 +416,7 @@ var MistralTextAdapter = class extends BaseTextAdapter {
305
416
  hasEmittedRunFinished = true;
306
417
  yield asChunk({
307
418
  type: "RUN_FINISHED",
419
+ ...responseId && { responseId },
308
420
  runId: aguiState.runId,
309
421
  threadId: aguiState.threadId,
310
422
  model: chunkModel,
@@ -312,7 +424,8 @@ var MistralTextAdapter = class extends BaseTextAdapter {
312
424
  usage: usage ? {
313
425
  promptTokens: usage.prompt_tokens || 0,
314
426
  completionTokens: usage.completion_tokens || 0,
315
- totalTokens: usage.total_tokens || 0
427
+ totalTokens: usage.total_tokens || 0,
428
+ ...mistralCachedTokens(usage)
316
429
  } : void 0,
317
430
  finishReason: computedFinishReason
318
431
  });
@@ -359,6 +472,7 @@ var MistralTextAdapter = class extends BaseTextAdapter {
359
472
  hasEmittedRunFinished = true;
360
473
  yield asChunk({
361
474
  type: "RUN_FINISHED",
475
+ ...responseId && { responseId },
362
476
  runId: aguiState.runId,
363
477
  threadId: aguiState.threadId,
364
478
  model: lastChunkModel,
@@ -418,10 +532,9 @@ var MistralTextAdapter = class extends BaseTextAdapter {
418
532
  * parses the SSE stream manually, bypassing the SDK's Zod validation which
419
533
  * rejects streaming tool call chunks that omit `name` in argument deltas.
420
534
  */
421
- async *fetchRawMistralStream(params, config, wrapFetch) {
535
+ async *fetchRawMistralStream(body, config, wrapFetch) {
422
536
  const serverURL = (config.baseURL ?? config.serverURL ?? "https://api.mistral.ai").replace(/\/+$/, "").replace(/\/v1$/, "");
423
537
  const url = config.resolveRequestUrl?.(true) ?? `${serverURL}/v1/chat/completions`;
424
- const body = this.toWireBody(params);
425
538
  const headers = {
426
539
  "Content-Type": "application/json",
427
540
  Authorization: `Bearer ${config.getAccessToken === void 0 ? config.apiKey : await config.getAccessToken()}`,
@@ -480,7 +593,7 @@ var MistralTextAdapter = class extends BaseTextAdapter {
480
593
  * snake_case wire body, including converting messages.
481
594
  */
482
595
  toWireBody(params) {
483
- const { messages, maxTokens, topP, randomSeed, responseFormat, toolChoice, parallelToolCalls, frequencyPenalty, presencePenalty, safePrompt, stream: _stream, ...rest } = params;
596
+ const { messages, maxTokens, topP, randomSeed, responseFormat, toolChoice, parallelToolCalls, frequencyPenalty, presencePenalty, safePrompt, reasoningEffort, promptMode, stream: _stream, ...rest } = params;
484
597
  return {
485
598
  ...rest,
486
599
  messages: messages.map(messageToWire),
@@ -494,7 +607,9 @@ var MistralTextAdapter = class extends BaseTextAdapter {
494
607
  ...parallelToolCalls != null && { parallel_tool_calls: parallelToolCalls },
495
608
  ...frequencyPenalty != null && { frequency_penalty: frequencyPenalty },
496
609
  ...presencePenalty != null && { presence_penalty: presencePenalty },
497
- ...safePrompt != null && { safe_prompt: safePrompt }
610
+ ...safePrompt != null && { safe_prompt: safePrompt },
611
+ ...reasoningEffort != null && { reasoning_effort: reasoningEffort },
612
+ ...promptMode != null && { prompt_mode: promptMode }
498
613
  };
499
614
  }
500
615
  /**
@@ -531,13 +646,24 @@ var MistralTextAdapter = class extends BaseTextAdapter {
531
646
  mapTextOptionsToMistral(options) {
532
647
  const modelOptions = options.modelOptions;
533
648
  const tools = options.tools ? convertToolsToProviderFormat(options.tools) : void 0;
649
+ const replay = transformMessagesForReplay(options.messages, {
650
+ provider: this.provider,
651
+ api: this.api,
652
+ model: options.model
653
+ }, (id, { attempt }) => {
654
+ const normalized = id.replace(/[^a-zA-Z0-9]/g, "");
655
+ if (attempt === 0 && normalized.length === 9) return normalized;
656
+ const seed = normalized || id;
657
+ return hashToolCallId(attempt === 0 ? seed : `${seed}:${attempt}`).replace(/[^a-zA-Z0-9]/g, "").slice(0, 9);
658
+ });
534
659
  const messages = [];
535
660
  if (options.systemPrompts && options.systemPrompts.length > 0) messages.push({
536
661
  role: "system",
537
662
  content: options.systemPrompts.join("\n")
538
663
  });
539
- for (const message of options.messages) messages.push(this.convertMessageToMistral(message));
664
+ for (const message of replay.messages) messages.push(this.convertMessageToMistral(message));
540
665
  return {
666
+ ...tools?.length && options.toolChoice !== void 0 ? { toolChoice: toMistralToolChoice(options.toolChoice) } : void 0,
541
667
  model: this.rawConfig.requestModel ?? options.model,
542
668
  messages,
543
669
  temperature: modelOptions?.temperature ?? void 0,
@@ -545,6 +671,7 @@ var MistralTextAdapter = class extends BaseTextAdapter {
545
671
  topP: modelOptions?.top_p ?? void 0,
546
672
  tools,
547
673
  stream: true,
674
+ ...mistralReasoning(options.model, options.reasoning, MISTRAL_MODEL_REASONING[options.model]),
548
675
  ...modelOptions && {
549
676
  ...modelOptions.stop != null && { stop: modelOptions.stop },
550
677
  ...modelOptions.random_seed != null && { randomSeed: modelOptions.random_seed },
@@ -582,7 +709,7 @@ var MistralTextAdapter = class extends BaseTextAdapter {
582
709
  }));
583
710
  return {
584
711
  role: "assistant",
585
- content: this.extractTextContent(message.content),
712
+ content: this.assistantContent(message),
586
713
  ...toolCalls && toolCalls.length > 0 ? { toolCalls } : {}
587
714
  };
588
715
  }
@@ -597,6 +724,30 @@ var MistralTextAdapter = class extends BaseTextAdapter {
597
724
  content: parts.length > 0 ? parts : ""
598
725
  };
599
726
  }
727
+ /** Readable thinking and text, in the stored block order. */
728
+ assistantContent(message) {
729
+ if (!message.thinking?.length) return this.extractTextContent(message.content);
730
+ const blocks = orderedAssistantBlocks(message) ?? [...message.thinking.map((thinking) => ({
731
+ type: "thinking",
732
+ thinking
733
+ })), {
734
+ type: "text",
735
+ text: this.extractTextContent(message.content)
736
+ }];
737
+ const content = [];
738
+ for (const block of blocks) if (block.type === "text") content.push({
739
+ type: "text",
740
+ text: block.text
741
+ });
742
+ else if (block.type === "thinking" && !block.thinking.redacted && block.thinking.content.trim()) content.push({
743
+ type: "thinking",
744
+ thinking: [{
745
+ type: "text",
746
+ text: block.thinking.content
747
+ }]
748
+ });
749
+ return content;
750
+ }
600
751
  /**
601
752
  * Converts a ContentPart to a Mistral content part. Returns undefined for
602
753
  * unsupported part types.