@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.
- package/README.md +93 -0
- package/dist/esm/adapters/text.d.ts +106 -0
- package/dist/esm/adapters/text.js +741 -0
- package/dist/esm/adapters/text.js.map +1 -0
- package/dist/esm/index.d.ts +10 -0
- package/dist/esm/index.js +9 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/message-types.d.ts +155 -0
- package/dist/esm/model-meta.d.ts +263 -0
- package/dist/esm/model-meta.js +50 -0
- package/dist/esm/model-meta.js.map +1 -0
- package/dist/esm/text/text-provider-options.d.ts +80 -0
- package/dist/esm/tools/function-tool.d.ts +13 -0
- package/dist/esm/tools/function-tool.js +26 -0
- package/dist/esm/tools/function-tool.js.map +1 -0
- package/dist/esm/tools/index.d.ts +2 -0
- package/dist/esm/tools/tool-converter.d.ts +6 -0
- package/dist/esm/tools/tool-converter.js +10 -0
- package/dist/esm/tools/tool-converter.js.map +1 -0
- package/dist/esm/utils/client.d.ts +24 -0
- package/dist/esm/utils/client.js +44 -0
- package/dist/esm/utils/client.js.map +1 -0
- package/dist/esm/utils/index.d.ts +2 -0
- package/dist/esm/utils/schema-converter.d.ts +18 -0
- package/dist/esm/utils/schema-converter.js +93 -0
- package/dist/esm/utils/schema-converter.js.map +1 -0
- package/package.json +60 -0
- package/src/adapters/text.ts +1141 -0
- package/src/index.ts +33 -0
- package/src/message-types.ts +194 -0
- package/src/model-meta.ts +285 -0
- package/src/text/text-provider-options.ts +103 -0
- package/src/tools/function-tool.ts +42 -0
- package/src/tools/index.ts +5 -0
- package/src/tools/tool-converter.ts +14 -0
- package/src/utils/client.ts +70 -0
- package/src/utils/index.ts +10 -0
- package/src/utils/schema-converter.ts +136 -0
package/README.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# @tanstack/ai-mistral
|
|
2
|
+
|
|
3
|
+
Mistral adapter for TanStack AI.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @tanstack/ai-mistral
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @tanstack/ai-mistral
|
|
11
|
+
# or
|
|
12
|
+
yarn add @tanstack/ai-mistral
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Setup
|
|
16
|
+
|
|
17
|
+
Get your API key from [Mistral Console](https://console.mistral.ai/) and set it as an environment variable:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
export MISTRAL_API_KEY="..."
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Text/Chat Adapter
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { mistralText } from '@tanstack/ai-mistral'
|
|
29
|
+
import { generate } from '@tanstack/ai'
|
|
30
|
+
|
|
31
|
+
const adapter = mistralText('mistral-large-latest')
|
|
32
|
+
|
|
33
|
+
const result = await generate({
|
|
34
|
+
adapter,
|
|
35
|
+
model: 'mistral-large-latest',
|
|
36
|
+
messages: [
|
|
37
|
+
{ role: 'user', content: 'Explain quantum computing in simple terms' },
|
|
38
|
+
],
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
console.log(result.text)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### With Explicit API Key
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { createMistralText } from '@tanstack/ai-mistral'
|
|
48
|
+
|
|
49
|
+
const adapter = createMistralText(
|
|
50
|
+
'mistral-large-latest',
|
|
51
|
+
process.env.MISTRAL_API_KEY!,
|
|
52
|
+
)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Supported Models
|
|
56
|
+
|
|
57
|
+
### Chat Models
|
|
58
|
+
|
|
59
|
+
- `mistral-large-latest` - Frontier flagship model (128k context)
|
|
60
|
+
- `mistral-medium-latest` - Balanced multimodal model (vision)
|
|
61
|
+
- `mistral-small-latest` - Fast, affordable multimodal model (vision)
|
|
62
|
+
- `ministral-8b-latest` - 8B edge model
|
|
63
|
+
- `ministral-3b-latest` - 3B edge model
|
|
64
|
+
- `codestral-latest` - Code-specialized model (256k context)
|
|
65
|
+
- `pixtral-large-latest` - Large vision model
|
|
66
|
+
- `pixtral-12b-2409` - 12B vision model
|
|
67
|
+
- `magistral-medium-latest` - Reasoning model
|
|
68
|
+
- `magistral-small-latest` - Small reasoning model
|
|
69
|
+
- `open-mistral-nemo` - Open 12B model
|
|
70
|
+
|
|
71
|
+
See [Mistral model comparison](https://docs.mistral.ai/getting-started/models/compare) for full details.
|
|
72
|
+
|
|
73
|
+
## Features
|
|
74
|
+
|
|
75
|
+
- ✅ Streaming chat completions
|
|
76
|
+
- ✅ Structured output (JSON Schema)
|
|
77
|
+
- ✅ Function/tool calling
|
|
78
|
+
- ✅ Reasoning (magistral-\_ models — streamed as `REASONING\__` events)
|
|
79
|
+
- ✅ Multimodal input (text + images) — requires a vision-capable model (`pixtral-large-latest`, `pixtral-12b-2409`, `mistral-medium-latest`, or `mistral-small-latest`)
|
|
80
|
+
- ❌ Embeddings (use [@mistralai/mistralai](https://github.com/mistralai/client-ts) directly)
|
|
81
|
+
- ❌ Image generation
|
|
82
|
+
|
|
83
|
+
## Tree-Shakeable Adapters
|
|
84
|
+
|
|
85
|
+
This package uses tree-shakeable adapters, so you only import what you need:
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import { mistralText } from '@tanstack/ai-mistral'
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
|
|
93
|
+
MIT
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { BaseTextAdapter, StructuredOutputOptions, StructuredOutputResult } from '@tanstack/ai/adapters';
|
|
2
|
+
import { Modality, StreamChunk, TextOptions } from '@tanstack/ai';
|
|
3
|
+
import { MISTRAL_CHAT_MODELS, MistralChatModelProviderOptionsByName, MistralModelInputModalitiesByName } from '../model-meta.js';
|
|
4
|
+
import { ExternalTextProviderOptions } from '../text/text-provider-options.js';
|
|
5
|
+
import { MistralMessageMetadataByModality } from '../message-types.js';
|
|
6
|
+
import { MistralClientConfig } from '../utils.js';
|
|
7
|
+
/**
|
|
8
|
+
* Configuration for Mistral text adapter.
|
|
9
|
+
*/
|
|
10
|
+
export type MistralTextConfig = MistralClientConfig;
|
|
11
|
+
/**
|
|
12
|
+
* Alias for TextProviderOptions for external use.
|
|
13
|
+
*/
|
|
14
|
+
export type MistralTextProviderOptions = ExternalTextProviderOptions;
|
|
15
|
+
type ResolveProviderOptions<TModel extends string> = TModel extends keyof MistralChatModelProviderOptionsByName ? MistralChatModelProviderOptionsByName[TModel] : MistralTextProviderOptions;
|
|
16
|
+
type ResolveInputModalities<TModel extends string> = TModel extends keyof MistralModelInputModalitiesByName ? MistralModelInputModalitiesByName[TModel] : readonly ['text'];
|
|
17
|
+
/**
|
|
18
|
+
* Mistral Text (Chat) Adapter.
|
|
19
|
+
*
|
|
20
|
+
* Tree-shakeable adapter for Mistral chat/text completion functionality.
|
|
21
|
+
*/
|
|
22
|
+
export declare class MistralTextAdapter<TModel extends (typeof MISTRAL_CHAT_MODELS)[number], TProviderOptions extends Record<string, any> = ResolveProviderOptions<TModel>, TInputModalities extends ReadonlyArray<Modality> = ResolveInputModalities<TModel>> extends BaseTextAdapter<TModel, TProviderOptions, TInputModalities, MistralMessageMetadataByModality> {
|
|
23
|
+
readonly name: "mistral";
|
|
24
|
+
private readonly client;
|
|
25
|
+
private readonly rawConfig;
|
|
26
|
+
constructor(config: MistralTextConfig, model: TModel);
|
|
27
|
+
chatStream(options: TextOptions<TProviderOptions>): AsyncIterable<StreamChunk>;
|
|
28
|
+
/**
|
|
29
|
+
* Generate structured output using Mistral's JSON Schema response format.
|
|
30
|
+
*/
|
|
31
|
+
structuredOutput(options: StructuredOutputOptions<TProviderOptions>): Promise<StructuredOutputResult<unknown>>;
|
|
32
|
+
/**
|
|
33
|
+
* Processes streaming chunks from the Mistral API and yields AG-UI stream events.
|
|
34
|
+
*/
|
|
35
|
+
private processMistralStreamChunks;
|
|
36
|
+
/**
|
|
37
|
+
* Makes a raw fetch request to the Mistral chat completions endpoint and
|
|
38
|
+
* parses the SSE stream manually, bypassing the SDK's Zod validation which
|
|
39
|
+
* rejects streaming tool call chunks that omit `name` in argument deltas.
|
|
40
|
+
*/
|
|
41
|
+
private fetchRawMistralStream;
|
|
42
|
+
/**
|
|
43
|
+
* Converts the SDK's camelCase `ChatCompletionStreamRequest` into the
|
|
44
|
+
* snake_case wire body, including converting messages.
|
|
45
|
+
*/
|
|
46
|
+
private toWireBody;
|
|
47
|
+
/**
|
|
48
|
+
* Splits a Mistral delta content payload into text and reasoning deltas.
|
|
49
|
+
* Mistral reasoning models (magistral-*) stream reasoning content as
|
|
50
|
+
* `{ type: 'thinking', thinking: [{ type: 'text', text }, ...] }` content
|
|
51
|
+
* parts. A single delta may contain text only, thinking only, or — rarely —
|
|
52
|
+
* both (when a step transitions); both fields are returned so the caller
|
|
53
|
+
* can sequence REASONING and TEXT lifecycle events in order.
|
|
54
|
+
*/
|
|
55
|
+
private extractDeltaParts;
|
|
56
|
+
/**
|
|
57
|
+
* Maps common TextOptions to Mistral Chat Completions request parameters.
|
|
58
|
+
*/
|
|
59
|
+
private mapTextOptionsToMistral;
|
|
60
|
+
/**
|
|
61
|
+
* Converts a TanStack AI ModelMessage to a Mistral ChatCompletionMessageParam.
|
|
62
|
+
*/
|
|
63
|
+
private convertMessageToMistral;
|
|
64
|
+
/**
|
|
65
|
+
* Converts a ContentPart to a Mistral content part. Returns undefined for
|
|
66
|
+
* unsupported part types.
|
|
67
|
+
*/
|
|
68
|
+
private convertContentPartToMistral;
|
|
69
|
+
/**
|
|
70
|
+
* Normalizes message content to an array of ContentPart.
|
|
71
|
+
*/
|
|
72
|
+
private normalizeContent;
|
|
73
|
+
/**
|
|
74
|
+
* Extracts text content from a content value that may be string, null, or ContentPart array.
|
|
75
|
+
*/
|
|
76
|
+
private extractTextContent;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Creates a Mistral text adapter with explicit API key.
|
|
80
|
+
*
|
|
81
|
+
* @param model - The model name (e.g., 'mistral-large-latest')
|
|
82
|
+
* @param apiKey - Your Mistral API key
|
|
83
|
+
* @param config - Optional additional configuration
|
|
84
|
+
* @returns Configured Mistral text adapter instance
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* const adapter = createMistralText('mistral-large-latest', 'api_key');
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export declare function createMistralText<TModel extends (typeof MISTRAL_CHAT_MODELS)[number]>(model: TModel, apiKey: string, config?: Omit<MistralTextConfig, 'apiKey'>): MistralTextAdapter<TModel>;
|
|
92
|
+
/**
|
|
93
|
+
* Creates a Mistral text adapter using the `MISTRAL_API_KEY` environment variable.
|
|
94
|
+
*
|
|
95
|
+
* @param model - The model name (e.g., 'mistral-large-latest')
|
|
96
|
+
* @param config - Optional configuration (excluding apiKey)
|
|
97
|
+
* @returns Configured Mistral text adapter instance
|
|
98
|
+
* @throws Error if MISTRAL_API_KEY is not found in environment
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```typescript
|
|
102
|
+
* const adapter = mistralText('mistral-large-latest');
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export declare function mistralText<TModel extends (typeof MISTRAL_CHAT_MODELS)[number]>(model: TModel, config?: Omit<MistralTextConfig, 'apiKey'>): MistralTextAdapter<TModel>;
|
|
106
|
+
export {};
|