@tanstack/ai-mistral 0.7.2 → 1.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.
- package/README.md +5 -7
- package/dist/esm/adapters/embedding.js +1 -1
- package/dist/esm/adapters/embedding.js.map +1 -1
- package/dist/esm/adapters/text.js +51 -36
- package/dist/esm/adapters/text.js.map +1 -1
- package/dist/esm/model-meta.d.ts +2 -65
- package/dist/esm/model-meta.js +1 -75
- package/dist/esm/model-meta.js.map +1 -1
- package/package.json +4 -4
- package/src/adapters/embedding.ts +5 -1
- package/src/adapters/text.ts +96 -43
- package/src/input-modalities.test.ts +1 -1
- package/src/model-meta.ts +1 -61
package/README.md
CHANGED
|
@@ -46,13 +46,14 @@ export MISTRAL_API_KEY="..."
|
|
|
46
46
|
|
|
47
47
|
```typescript
|
|
48
48
|
import { mistralText } from '@tanstack/ai-mistral'
|
|
49
|
-
import {
|
|
49
|
+
import { chat } from '@tanstack/ai'
|
|
50
50
|
|
|
51
51
|
const adapter = mistralText('mistral-large-latest')
|
|
52
52
|
|
|
53
|
-
const result = await
|
|
53
|
+
const result = await chat({
|
|
54
54
|
adapter,
|
|
55
|
-
|
|
55
|
+
threadId: 'thread-1',
|
|
56
|
+
stream: false,
|
|
56
57
|
messages: [
|
|
57
58
|
{ role: 'user', content: 'Explain quantum computing in simple terms' },
|
|
58
59
|
],
|
|
@@ -82,11 +83,8 @@ const adapter = createMistralText(
|
|
|
82
83
|
- `ministral-8b-latest` - 8B edge model
|
|
83
84
|
- `ministral-3b-latest` - 3B edge model
|
|
84
85
|
- `codestral-latest` - Code-specialized model (256k context)
|
|
85
|
-
- `pixtral-large-latest` - Large vision model
|
|
86
|
-
- `pixtral-12b-2409` - 12B vision model
|
|
87
86
|
- `magistral-medium-latest` - Reasoning model
|
|
88
87
|
- `magistral-small-latest` - Small reasoning model
|
|
89
|
-
- `open-mistral-nemo` - Open 12B model
|
|
90
88
|
|
|
91
89
|
See [Mistral model comparison](https://docs.mistral.ai/getting-started/models/compare) for full details.
|
|
92
90
|
|
|
@@ -96,7 +94,7 @@ See [Mistral model comparison](https://docs.mistral.ai/getting-started/models/co
|
|
|
96
94
|
- ✅ Structured output (JSON Schema)
|
|
97
95
|
- ✅ Function/tool calling
|
|
98
96
|
- ✅ Reasoning (magistral-\_ models — streamed as `REASONING\__` events)
|
|
99
|
-
- ✅ Multimodal input (text + images) — requires a vision-capable model (`
|
|
97
|
+
- ✅ Multimodal input (text + images) — requires a vision-capable model (`mistral-medium-latest` or `mistral-small-latest`)
|
|
100
98
|
- ❌ Embeddings (use [@mistralai/mistralai](https://github.com/mistralai/client-ts) directly)
|
|
101
99
|
- ❌ Image generation
|
|
102
100
|
|
|
@@ -37,7 +37,7 @@ var MistralEmbeddingAdapter = class extends BaseEmbeddingAdapter {
|
|
|
37
37
|
provider: this.name,
|
|
38
38
|
model
|
|
39
39
|
});
|
|
40
|
-
const response = await this.client.embeddings.create(request);
|
|
40
|
+
const response = options.abortSignal ? await this.client.embeddings.create(request, { fetchOptions: { signal: options.abortSignal } }) : await this.client.embeddings.create(request);
|
|
41
41
|
const usage = {
|
|
42
42
|
promptTokens: response.usage.promptTokens ?? 0,
|
|
43
43
|
completionTokens: 0,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"embedding.js","names":[],"sources":["../../../src/adapters/embedding.ts"],"sourcesContent":["import { BaseEmbeddingAdapter } from '@tanstack/ai/adapters'\nimport { toRunErrorPayload } from '@tanstack/ai/adapter-internals'\nimport { requireTextOnlyEmbeddingInput } from '@tanstack/ai'\nimport {\n createMistralClient,\n generateId,\n getMistralApiKeyFromEnv,\n} from '../utils/client'\nimport type {\n EmbeddingOptions,\n EmbeddingResult,\n TokenUsage,\n} from '@tanstack/ai'\nimport type { Mistral } from '@mistralai/mistralai'\nimport type { EmbeddingRequest } from '@mistralai/mistralai/models/components'\nimport type {\n MistralEmbeddingModel,\n MistralEmbeddingModelInputModalitiesByName,\n MistralEmbeddingModelProviderOptionsByName,\n} from '../model-meta'\nimport type { MistralEmbeddingProviderOptions } from '../embedding/embedding-provider-options'\nimport type { MistralClientConfig } from '../utils/client'\n\n/**\n * Configuration for Mistral embedding adapter.\n */\nexport type MistralEmbeddingConfig = MistralClientConfig\n\n/**\n * Mistral Embedding Adapter\n *\n * Tree-shakeable adapter for Mistral text embeddings.\n * Supports mistral-embed and codestral-embed.\n *\n * Features:\n * - Batch embedding (one request for the whole input array)\n * - Dimension reduction for codestral-embed via the top-level `dimensions`\n * option (mapped to Mistral's `outputDimension`); mistral-embed has a fixed\n * 1024-dimension output and rejects `dimensions`.\n */\nexport class MistralEmbeddingAdapter<\n TModel extends MistralEmbeddingModel,\n> extends BaseEmbeddingAdapter<\n TModel,\n MistralEmbeddingProviderOptions,\n MistralEmbeddingModelProviderOptionsByName,\n MistralEmbeddingModelInputModalitiesByName\n> {\n readonly name = 'mistral' as const\n\n protected client: Mistral\n\n constructor(config: MistralEmbeddingConfig, model: TModel) {\n super(model, {})\n this.client = createMistralClient(config)\n }\n\n async createEmbeddings(\n options: EmbeddingOptions<MistralEmbeddingProviderOptions>,\n ): Promise<EmbeddingResult> {\n const { model, logger, modelOptions } = options\n const texts = requireTextOnlyEmbeddingInput(options.input, this.name, model)\n\n if (options.dimensions !== undefined && model === 'mistral-embed') {\n throw new Error(\n 'mistral-embed does not support requesting dimensions (output is a fixed 1024-dimension vector). Use codestral-embed for dimension reduction, or omit `dimensions`.',\n )\n }\n\n try {\n // Spread modelOptions first so it can never override the validated\n // fields (server routes often pass modelOptions through from untyped\n // client input).\n const request: EmbeddingRequest = {\n ...modelOptions,\n model,\n inputs: texts,\n }\n if (options.dimensions !== undefined) {\n request.outputDimension = options.dimensions\n }\n\n logger.request(\n `activity=embed provider=${this.name} model=${model} inputs=${texts.length}`,\n { provider: this.name, model },\n )\n\n const response = await this.client.embeddings.create(request)\n\n const usage: TokenUsage = {\n promptTokens: response.usage.promptTokens ?? 0,\n completionTokens: 0,\n totalTokens: response.usage.totalTokens ?? 0,\n }\n\n return {\n id: generateId(this.name),\n model,\n // Mistral returns one entry per input; `index` is optional in the SDK\n // types, so fall back to array order when it's absent.\n embeddings: response.data.map((item, arrayIndex) => ({\n vector: item.embedding ?? [],\n index: item.index ?? arrayIndex,\n })),\n usage,\n }\n } catch (error: unknown) {\n logger.errors(`${this.name}.createEmbeddings fatal`, {\n error: toRunErrorPayload(error, `${this.name}.createEmbeddings failed`),\n source: `${this.name}.createEmbeddings`,\n })\n throw error\n }\n }\n}\n\n/**\n * Creates a Mistral embedding adapter with explicit API key.\n * Type resolution happens here at the call site.\n *\n * @param model - The model name (e.g., 'mistral-embed')\n * @param apiKey - Your Mistral API key\n * @param config - Optional additional configuration\n * @returns Configured Mistral embedding adapter instance with resolved types\n *\n * @example\n * ```typescript\n * const adapter = createMistralEmbedding('mistral-embed', 'api_key');\n *\n * const result = await embed({\n * adapter,\n * input: 'a red guitar'\n * });\n * ```\n */\nexport function createMistralEmbedding<TModel extends MistralEmbeddingModel>(\n model: TModel,\n apiKey: string,\n config?: Omit<MistralEmbeddingConfig, 'apiKey'>,\n): MistralEmbeddingAdapter<TModel> {\n return new MistralEmbeddingAdapter({ apiKey, ...config }, model)\n}\n\n/**\n * Creates a Mistral embedding adapter using the `MISTRAL_API_KEY` environment variable.\n * Type resolution happens here at the call site.\n *\n * Looks for `MISTRAL_API_KEY` in:\n * - `process.env` (Node.js)\n * - `window.env` (Browser with injected env)\n *\n * @param model - The model name (e.g., 'codestral-embed')\n * @param config - Optional configuration (excluding apiKey which is auto-detected)\n * @returns Configured Mistral embedding adapter instance with resolved types\n * @throws Error if MISTRAL_API_KEY is not found in environment\n *\n * @example\n * ```typescript\n * // Automatically uses MISTRAL_API_KEY from environment\n * const adapter = mistralEmbedding('codestral-embed');\n *\n * const result = await embed({\n * adapter,\n * input: ['a red guitar', 'a blue drum kit'],\n * dimensions: 256\n * });\n *\n * console.log(result.embeddings[0].vector)\n * ```\n */\nexport function mistralEmbedding<TModel extends MistralEmbeddingModel>(\n model: TModel,\n config?: Omit<MistralEmbeddingConfig, 'apiKey'>,\n): MistralEmbeddingAdapter<TModel> {\n const apiKey = getMistralApiKeyFromEnv()\n return createMistralEmbedding(model, apiKey, config)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAwCA,IAAa,0BAAb,cAEU,qBAKR;CACA,OAAgB;CAEhB;CAEA,YAAY,QAAgC,OAAe;EACzD,MAAM,OAAO,CAAC,CAAC;EACf,KAAK,SAAS,oBAAoB,MAAM;CAC1C;CAEA,MAAM,iBACJ,SAC0B;EAC1B,MAAM,EAAE,OAAO,QAAQ,iBAAiB;EACxC,MAAM,QAAQ,8BAA8B,QAAQ,OAAO,KAAK,MAAM,KAAK;EAE3E,IAAI,QAAQ,eAAe,KAAA,KAAa,UAAU,iBAChD,MAAM,IAAI,MACR,oKACF;EAGF,IAAI;GAIF,MAAM,UAA4B;IAChC,GAAG;IACH;IACA,QAAQ;GACV;GACA,IAAI,QAAQ,eAAe,KAAA,GACzB,QAAQ,kBAAkB,QAAQ;GAGpC,OAAO,QACL,2BAA2B,KAAK,KAAK,SAAS,MAAM,UAAU,MAAM,UACpE;IAAE,UAAU,KAAK;IAAM;GAAM,CAC/B;GAEA,MAAM,WAAW,MAAM,KAAK,OAAO,WAAW,OAAO,OAAO;
|
|
1
|
+
{"version":3,"file":"embedding.js","names":[],"sources":["../../../src/adapters/embedding.ts"],"sourcesContent":["import { BaseEmbeddingAdapter } from '@tanstack/ai/adapters'\nimport { toRunErrorPayload } from '@tanstack/ai/adapter-internals'\nimport { requireTextOnlyEmbeddingInput } from '@tanstack/ai'\nimport {\n createMistralClient,\n generateId,\n getMistralApiKeyFromEnv,\n} from '../utils/client'\nimport type {\n EmbeddingOptions,\n EmbeddingResult,\n TokenUsage,\n} from '@tanstack/ai'\nimport type { Mistral } from '@mistralai/mistralai'\nimport type { EmbeddingRequest } from '@mistralai/mistralai/models/components'\nimport type {\n MistralEmbeddingModel,\n MistralEmbeddingModelInputModalitiesByName,\n MistralEmbeddingModelProviderOptionsByName,\n} from '../model-meta'\nimport type { MistralEmbeddingProviderOptions } from '../embedding/embedding-provider-options'\nimport type { MistralClientConfig } from '../utils/client'\n\n/**\n * Configuration for Mistral embedding adapter.\n */\nexport type MistralEmbeddingConfig = MistralClientConfig\n\n/**\n * Mistral Embedding Adapter\n *\n * Tree-shakeable adapter for Mistral text embeddings.\n * Supports mistral-embed and codestral-embed.\n *\n * Features:\n * - Batch embedding (one request for the whole input array)\n * - Dimension reduction for codestral-embed via the top-level `dimensions`\n * option (mapped to Mistral's `outputDimension`); mistral-embed has a fixed\n * 1024-dimension output and rejects `dimensions`.\n */\nexport class MistralEmbeddingAdapter<\n TModel extends MistralEmbeddingModel,\n> extends BaseEmbeddingAdapter<\n TModel,\n MistralEmbeddingProviderOptions,\n MistralEmbeddingModelProviderOptionsByName,\n MistralEmbeddingModelInputModalitiesByName\n> {\n readonly name = 'mistral' as const\n\n protected client: Mistral\n\n constructor(config: MistralEmbeddingConfig, model: TModel) {\n super(model, {})\n this.client = createMistralClient(config)\n }\n\n async createEmbeddings(\n options: EmbeddingOptions<MistralEmbeddingProviderOptions>,\n ): Promise<EmbeddingResult> {\n const { model, logger, modelOptions } = options\n const texts = requireTextOnlyEmbeddingInput(options.input, this.name, model)\n\n if (options.dimensions !== undefined && model === 'mistral-embed') {\n throw new Error(\n 'mistral-embed does not support requesting dimensions (output is a fixed 1024-dimension vector). Use codestral-embed for dimension reduction, or omit `dimensions`.',\n )\n }\n\n try {\n // Spread modelOptions first so it can never override the validated\n // fields (server routes often pass modelOptions through from untyped\n // client input).\n const request: EmbeddingRequest = {\n ...modelOptions,\n model,\n inputs: texts,\n }\n if (options.dimensions !== undefined) {\n request.outputDimension = options.dimensions\n }\n\n logger.request(\n `activity=embed provider=${this.name} model=${model} inputs=${texts.length}`,\n { provider: this.name, model },\n )\n\n const response = options.abortSignal\n ? await this.client.embeddings.create(request, {\n fetchOptions: { signal: options.abortSignal },\n })\n : await this.client.embeddings.create(request)\n\n const usage: TokenUsage = {\n promptTokens: response.usage.promptTokens ?? 0,\n completionTokens: 0,\n totalTokens: response.usage.totalTokens ?? 0,\n }\n\n return {\n id: generateId(this.name),\n model,\n // Mistral returns one entry per input; `index` is optional in the SDK\n // types, so fall back to array order when it's absent.\n embeddings: response.data.map((item, arrayIndex) => ({\n vector: item.embedding ?? [],\n index: item.index ?? arrayIndex,\n })),\n usage,\n }\n } catch (error: unknown) {\n logger.errors(`${this.name}.createEmbeddings fatal`, {\n error: toRunErrorPayload(error, `${this.name}.createEmbeddings failed`),\n source: `${this.name}.createEmbeddings`,\n })\n throw error\n }\n }\n}\n\n/**\n * Creates a Mistral embedding adapter with explicit API key.\n * Type resolution happens here at the call site.\n *\n * @param model - The model name (e.g., 'mistral-embed')\n * @param apiKey - Your Mistral API key\n * @param config - Optional additional configuration\n * @returns Configured Mistral embedding adapter instance with resolved types\n *\n * @example\n * ```typescript\n * const adapter = createMistralEmbedding('mistral-embed', 'api_key');\n *\n * const result = await embed({\n * adapter,\n * input: 'a red guitar'\n * });\n * ```\n */\nexport function createMistralEmbedding<TModel extends MistralEmbeddingModel>(\n model: TModel,\n apiKey: string,\n config?: Omit<MistralEmbeddingConfig, 'apiKey'>,\n): MistralEmbeddingAdapter<TModel> {\n return new MistralEmbeddingAdapter({ apiKey, ...config }, model)\n}\n\n/**\n * Creates a Mistral embedding adapter using the `MISTRAL_API_KEY` environment variable.\n * Type resolution happens here at the call site.\n *\n * Looks for `MISTRAL_API_KEY` in:\n * - `process.env` (Node.js)\n * - `window.env` (Browser with injected env)\n *\n * @param model - The model name (e.g., 'codestral-embed')\n * @param config - Optional configuration (excluding apiKey which is auto-detected)\n * @returns Configured Mistral embedding adapter instance with resolved types\n * @throws Error if MISTRAL_API_KEY is not found in environment\n *\n * @example\n * ```typescript\n * // Automatically uses MISTRAL_API_KEY from environment\n * const adapter = mistralEmbedding('codestral-embed');\n *\n * const result = await embed({\n * adapter,\n * input: ['a red guitar', 'a blue drum kit'],\n * dimensions: 256\n * });\n *\n * console.log(result.embeddings[0].vector)\n * ```\n */\nexport function mistralEmbedding<TModel extends MistralEmbeddingModel>(\n model: TModel,\n config?: Omit<MistralEmbeddingConfig, 'apiKey'>,\n): MistralEmbeddingAdapter<TModel> {\n const apiKey = getMistralApiKeyFromEnv()\n return createMistralEmbedding(model, apiKey, config)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAwCA,IAAa,0BAAb,cAEU,qBAKR;CACA,OAAgB;CAEhB;CAEA,YAAY,QAAgC,OAAe;EACzD,MAAM,OAAO,CAAC,CAAC;EACf,KAAK,SAAS,oBAAoB,MAAM;CAC1C;CAEA,MAAM,iBACJ,SAC0B;EAC1B,MAAM,EAAE,OAAO,QAAQ,iBAAiB;EACxC,MAAM,QAAQ,8BAA8B,QAAQ,OAAO,KAAK,MAAM,KAAK;EAE3E,IAAI,QAAQ,eAAe,KAAA,KAAa,UAAU,iBAChD,MAAM,IAAI,MACR,oKACF;EAGF,IAAI;GAIF,MAAM,UAA4B;IAChC,GAAG;IACH;IACA,QAAQ;GACV;GACA,IAAI,QAAQ,eAAe,KAAA,GACzB,QAAQ,kBAAkB,QAAQ;GAGpC,OAAO,QACL,2BAA2B,KAAK,KAAK,SAAS,MAAM,UAAU,MAAM,UACpE;IAAE,UAAU,KAAK;IAAM;GAAM,CAC/B;GAEA,MAAM,WAAW,QAAQ,cACrB,MAAM,KAAK,OAAO,WAAW,OAAO,SAAS,EAC3C,cAAc,EAAE,QAAQ,QAAQ,YAAY,EAC9C,CAAC,IACD,MAAM,KAAK,OAAO,WAAW,OAAO,OAAO;GAE/C,MAAM,QAAoB;IACxB,cAAc,SAAS,MAAM,gBAAgB;IAC7C,kBAAkB;IAClB,aAAa,SAAS,MAAM,eAAe;GAC7C;GAEA,OAAO;IACL,IAAI,WAAW,KAAK,IAAI;IACxB;IAGA,YAAY,SAAS,KAAK,KAAK,MAAM,gBAAgB;KACnD,QAAQ,KAAK,aAAa,CAAC;KAC3B,OAAO,KAAK,SAAS;IACvB,EAAE;IACF;GACF;EACF,SAAS,OAAgB;GACvB,OAAO,OAAO,GAAG,KAAK,KAAK,0BAA0B;IACnD,OAAO,kBAAkB,OAAO,GAAG,KAAK,KAAK,yBAAyB;IACtE,QAAQ,GAAG,KAAK,KAAK;GACvB,CAAC;GACD,MAAM;EACR;CACF;AACF;;;;;;;;;;;;;;;;;;;;AAqBA,SAAgB,uBACd,OACA,QACA,QACiC;CACjC,OAAO,IAAI,wBAAwB;EAAE;EAAQ,GAAG;CAAO,GAAG,KAAK;AACjE;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,SAAgB,iBACd,OACA,QACiC;CAEjC,OAAO,uBAAuB,OADf,wBACsB,GAAQ,MAAM;AACrD"}
|
|
@@ -121,7 +121,7 @@ var MistralTextAdapter = class extends BaseTextAdapter {
|
|
|
121
121
|
const timestamp = Date.now();
|
|
122
122
|
const aguiState = {
|
|
123
123
|
runId: options.runId ?? generateId(this.name),
|
|
124
|
-
threadId: options.threadId
|
|
124
|
+
threadId: options.threadId,
|
|
125
125
|
messageId: generateId(this.name),
|
|
126
126
|
timestamp,
|
|
127
127
|
hasEmittedRunStarted: false
|
|
@@ -132,7 +132,7 @@ var MistralTextAdapter = class extends BaseTextAdapter {
|
|
|
132
132
|
...this.toWireBody(requestParams),
|
|
133
133
|
...cacheKey !== void 0 && { prompt_cache_key: cacheKey }
|
|
134
134
|
};
|
|
135
|
-
const stream = this.fetchRawMistralStream(body, this.rawConfig, options.wrapFetch);
|
|
135
|
+
const stream = this.fetchRawMistralStream(body, this.rawConfig, options.wrapFetch, options.request?.signal ?? void 0);
|
|
136
136
|
for await (const chunk of this.processMistralStreamChunks(stream, options, aguiState)) yield chunk.type === "RUN_STARTED" || chunk.type === "RUN_ERROR" ? {
|
|
137
137
|
...chunk,
|
|
138
138
|
metadata: {
|
|
@@ -179,7 +179,8 @@ var MistralTextAdapter = class extends BaseTextAdapter {
|
|
|
179
179
|
error: {
|
|
180
180
|
message: err.message || "Unknown error",
|
|
181
181
|
code: err.code
|
|
182
|
-
}
|
|
182
|
+
},
|
|
183
|
+
...aguiState.usage && { usage: aguiState.usage }
|
|
183
184
|
});
|
|
184
185
|
throw err;
|
|
185
186
|
}
|
|
@@ -193,7 +194,10 @@ var MistralTextAdapter = class extends BaseTextAdapter {
|
|
|
193
194
|
const { schema: jsonSchema, nullWideningMap, strict } = makeMistralStructuredOutputCompatibleWithMap(outputSchema, outputSchema.required || []);
|
|
194
195
|
const cacheKey = mistralOwnApi(this.rawConfig, this.provider) ? mistralPromptCacheKey(chatOptions.promptCache) : void 0;
|
|
195
196
|
const next = chatOptions.wrapFetch ? chatOptions.wrapFetch(fetch) : fetch;
|
|
196
|
-
const
|
|
197
|
+
const client = cacheKey ? createMistralClient(this.rawConfig, withPromptCacheKey(next, cacheKey)) : chatOptions.wrapFetch ? createMistralClient(this.rawConfig, next) : this.client;
|
|
198
|
+
const signal = chatOptions.request?.signal;
|
|
199
|
+
const timeoutMs = this.rawConfig.timeoutMs || 3e4;
|
|
200
|
+
const params = {
|
|
197
201
|
...nonStreamParams,
|
|
198
202
|
responseFormat: {
|
|
199
203
|
type: "json_schema",
|
|
@@ -203,28 +207,29 @@ var MistralTextAdapter = class extends BaseTextAdapter {
|
|
|
203
207
|
strict
|
|
204
208
|
}
|
|
205
209
|
}
|
|
206
|
-
}
|
|
207
|
-
|
|
210
|
+
};
|
|
211
|
+
const response = signal ? await client.chat.complete(params, { signal: timeoutMs > 0 ? AbortSignal.any([signal, AbortSignal.timeout(timeoutMs)]) : signal }) : await client.chat.complete(params);
|
|
212
|
+
const usage = response.usage && {
|
|
213
|
+
promptTokens: response.usage.promptTokens ?? 0,
|
|
214
|
+
completionTokens: response.usage.completionTokens ?? 0,
|
|
215
|
+
totalTokens: response.usage.totalTokens ?? 0,
|
|
216
|
+
...mistralCachedTokens(response.usage)
|
|
217
|
+
};
|
|
218
|
+
if (response.choices[0]?.finishReason === "length") throw Object.assign(/* @__PURE__ */ new Error("mistral.structuredOutput: the response was cut off because the maximum token limit was reached (finish_reason=length); raise modelOptions.max_tokens"), usage && { usage });
|
|
208
219
|
const rawText = response.choices[0]?.message?.content;
|
|
209
220
|
const textContent = typeof rawText === "string" ? rawText : "";
|
|
210
221
|
let parsed;
|
|
211
222
|
try {
|
|
212
223
|
parsed = JSON.parse(textContent);
|
|
213
224
|
} catch {
|
|
214
|
-
throw new Error(`Failed to parse structured output as JSON. Content: ${textContent.slice(0, 200)}${textContent.length > 200 ? "..." : ""}`);
|
|
225
|
+
throw Object.assign(/* @__PURE__ */ new Error(`Failed to parse structured output as JSON. Content: ${textContent.slice(0, 200)}${textContent.length > 200 ? "..." : ""}`), usage && { usage });
|
|
215
226
|
}
|
|
216
|
-
const usage = response.usage;
|
|
217
227
|
return {
|
|
218
228
|
data: undoNullWidening(parsed, nullWideningMap),
|
|
219
229
|
...response.id && { responseId: response.id },
|
|
220
230
|
...response.model && { model: response.model },
|
|
221
231
|
rawText: textContent,
|
|
222
|
-
...usage && { usage
|
|
223
|
-
promptTokens: usage.promptTokens ?? 0,
|
|
224
|
-
completionTokens: usage.completionTokens ?? 0,
|
|
225
|
-
totalTokens: usage.totalTokens ?? 0,
|
|
226
|
-
...mistralCachedTokens(usage)
|
|
227
|
-
} }
|
|
232
|
+
...usage && { usage }
|
|
228
233
|
};
|
|
229
234
|
}
|
|
230
235
|
/**
|
|
@@ -232,6 +237,7 @@ var MistralTextAdapter = class extends BaseTextAdapter {
|
|
|
232
237
|
*/
|
|
233
238
|
async *processMistralStreamChunks(stream, options, aguiState) {
|
|
234
239
|
let accumulatedContent = "";
|
|
240
|
+
let finished;
|
|
235
241
|
const timestamp = aguiState.timestamp;
|
|
236
242
|
let hasEmittedTextMessageStart = false;
|
|
237
243
|
let hasEmittedTextMessageEnd = false;
|
|
@@ -247,6 +253,12 @@ var MistralTextAdapter = class extends BaseTextAdapter {
|
|
|
247
253
|
for await (const chunk of stream) {
|
|
248
254
|
lastChunkModel = chunk.model || lastChunkModel;
|
|
249
255
|
if (chunk.id) responseId = chunk.id;
|
|
256
|
+
if (chunk.usage) aguiState.usage = {
|
|
257
|
+
promptTokens: chunk.usage.prompt_tokens || 0,
|
|
258
|
+
completionTokens: chunk.usage.completion_tokens || 0,
|
|
259
|
+
totalTokens: chunk.usage.total_tokens || 0,
|
|
260
|
+
...mistralCachedTokens(chunk.usage)
|
|
261
|
+
};
|
|
250
262
|
const choice = chunk.choices?.[0];
|
|
251
263
|
if (!choice) continue;
|
|
252
264
|
const chunkModel = chunk.model || options.model;
|
|
@@ -327,7 +339,15 @@ var MistralTextAdapter = class extends BaseTextAdapter {
|
|
|
327
339
|
});
|
|
328
340
|
}
|
|
329
341
|
if (deltaToolCalls) for (const [i, toolCallDelta] of deltaToolCalls.entries()) {
|
|
330
|
-
|
|
342
|
+
let index = toolCallDelta.index ?? i;
|
|
343
|
+
if (toolCallDelta.id) {
|
|
344
|
+
const occupant = toolCallsInProgress.get(index);
|
|
345
|
+
if (occupant?.id && occupant.id !== toolCallDelta.id) {
|
|
346
|
+
const sameId = [...toolCallsInProgress].find(([, call]) => call.id === toolCallDelta.id);
|
|
347
|
+
if (sameId) index = sameId[0];
|
|
348
|
+
else while (toolCallsInProgress.has(index)) index += 1;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
331
351
|
let toolCall = toolCallsInProgress.get(index);
|
|
332
352
|
if (!toolCall) {
|
|
333
353
|
toolCall = {
|
|
@@ -350,7 +370,6 @@ var MistralTextAdapter = class extends BaseTextAdapter {
|
|
|
350
370
|
type: "TOOL_CALL_START",
|
|
351
371
|
toolCallId: toolCall.id,
|
|
352
372
|
toolCallName: toolCall.name,
|
|
353
|
-
toolName: toolCall.name,
|
|
354
373
|
model: chunkModel,
|
|
355
374
|
timestamp,
|
|
356
375
|
index
|
|
@@ -381,7 +400,6 @@ var MistralTextAdapter = class extends BaseTextAdapter {
|
|
|
381
400
|
type: "TOOL_CALL_END",
|
|
382
401
|
toolCallId: toolCall.id,
|
|
383
402
|
toolCallName: toolCall.name,
|
|
384
|
-
toolName: toolCall.name,
|
|
385
403
|
model: chunkModel,
|
|
386
404
|
timestamp,
|
|
387
405
|
args: toolCall.arguments,
|
|
@@ -413,25 +431,23 @@ var MistralTextAdapter = class extends BaseTextAdapter {
|
|
|
413
431
|
timestamp
|
|
414
432
|
});
|
|
415
433
|
}
|
|
416
|
-
const usage = chunk.usage;
|
|
417
434
|
hasEmittedRunFinished = true;
|
|
418
|
-
|
|
419
|
-
type: "RUN_FINISHED",
|
|
420
|
-
...responseId && { responseId },
|
|
421
|
-
runId: aguiState.runId,
|
|
422
|
-
threadId: aguiState.threadId,
|
|
435
|
+
finished = {
|
|
423
436
|
model: chunkModel,
|
|
424
|
-
timestamp,
|
|
425
|
-
usage: usage ? {
|
|
426
|
-
promptTokens: usage.prompt_tokens || 0,
|
|
427
|
-
completionTokens: usage.completion_tokens || 0,
|
|
428
|
-
totalTokens: usage.total_tokens || 0,
|
|
429
|
-
...mistralCachedTokens(usage)
|
|
430
|
-
} : void 0,
|
|
431
437
|
finishReason: computedFinishReason
|
|
432
|
-
}
|
|
438
|
+
};
|
|
433
439
|
}
|
|
434
440
|
}
|
|
441
|
+
if (finished) yield asChunk({
|
|
442
|
+
type: "RUN_FINISHED",
|
|
443
|
+
...responseId && { responseId },
|
|
444
|
+
runId: aguiState.runId,
|
|
445
|
+
threadId: aguiState.threadId,
|
|
446
|
+
model: finished.model,
|
|
447
|
+
timestamp,
|
|
448
|
+
...aguiState.usage && { usage: aguiState.usage },
|
|
449
|
+
finishReason: finished.finishReason
|
|
450
|
+
});
|
|
435
451
|
if (!hasEmittedRunFinished) {
|
|
436
452
|
if (reasoningMessageId !== null && !hasClosedReasoning) {
|
|
437
453
|
hasClosedReasoning = true;
|
|
@@ -456,7 +472,6 @@ var MistralTextAdapter = class extends BaseTextAdapter {
|
|
|
456
472
|
type: "TOOL_CALL_END",
|
|
457
473
|
toolCallId: toolCall.id,
|
|
458
474
|
toolCallName: toolCall.name,
|
|
459
|
-
toolName: toolCall.name,
|
|
460
475
|
model: lastChunkModel,
|
|
461
476
|
timestamp,
|
|
462
477
|
args: toolCall.arguments,
|
|
@@ -480,7 +495,7 @@ var MistralTextAdapter = class extends BaseTextAdapter {
|
|
|
480
495
|
threadId: aguiState.threadId,
|
|
481
496
|
model: lastChunkModel,
|
|
482
497
|
timestamp,
|
|
483
|
-
usage:
|
|
498
|
+
...aguiState.usage && { usage: aguiState.usage },
|
|
484
499
|
finishReason: hasEmittedToolCall ? "tool_calls" : "stop"
|
|
485
500
|
});
|
|
486
501
|
}
|
|
@@ -521,7 +536,6 @@ var MistralTextAdapter = class extends BaseTextAdapter {
|
|
|
521
536
|
type: "TOOL_CALL_END",
|
|
522
537
|
toolCallId: toolCall.id,
|
|
523
538
|
toolCallName: toolCall.name,
|
|
524
|
-
toolName: toolCall.name,
|
|
525
539
|
model: lastChunkModel,
|
|
526
540
|
timestamp,
|
|
527
541
|
args: toolCall.arguments,
|
|
@@ -536,7 +550,7 @@ var MistralTextAdapter = class extends BaseTextAdapter {
|
|
|
536
550
|
* parses the SSE stream manually, bypassing the SDK's Zod validation which
|
|
537
551
|
* rejects streaming tool call chunks that omit `name` in argument deltas.
|
|
538
552
|
*/
|
|
539
|
-
async *fetchRawMistralStream(body, config, wrapFetch) {
|
|
553
|
+
async *fetchRawMistralStream(body, config, wrapFetch, signal) {
|
|
540
554
|
const serverURL = (config.baseURL ?? config.serverURL ?? "https://api.mistral.ai").replace(/\/+$/, "").replace(/\/v1$/, "");
|
|
541
555
|
const url = config.resolveRequestUrl?.(true) ?? `${serverURL}/v1/chat/completions`;
|
|
542
556
|
const headers = {
|
|
@@ -547,7 +561,8 @@ var MistralTextAdapter = class extends BaseTextAdapter {
|
|
|
547
561
|
const response = await (wrapFetch ? wrapFetch(fetch) : fetch)(url, {
|
|
548
562
|
method: "POST",
|
|
549
563
|
headers,
|
|
550
|
-
body: JSON.stringify(body)
|
|
564
|
+
body: JSON.stringify(body),
|
|
565
|
+
signal
|
|
551
566
|
});
|
|
552
567
|
if (!response.ok) {
|
|
553
568
|
const errorText = await response.text();
|