@tanstack/ai-mistral 0.3.3 → 0.4.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/dist/esm/adapters/text.d.ts +2 -2
- package/dist/esm/adapters/text.js +16 -12
- package/dist/esm/adapters/text.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.js +2 -2
- package/dist/esm/model-meta.d.ts +71 -0
- package/dist/esm/model-meta.js +335 -242
- package/dist/esm/model-meta.js.map +1 -1
- package/dist/esm/tools/function-tool.d.ts +4 -5
- package/dist/esm/tools/function-tool.js +7 -8
- package/dist/esm/tools/function-tool.js.map +1 -1
- package/dist/esm/tools/function-tool.test.d.ts +1 -0
- package/dist/esm/utils/client.d.ts +12 -0
- package/dist/esm/utils/client.js +9 -5
- package/dist/esm/utils/client.js.map +1 -1
- package/dist/esm/utils/schema-converter.d.ts +15 -15
- package/dist/esm/utils/schema-converter.js +173 -50
- package/dist/esm/utils/schema-converter.js.map +1 -1
- package/dist/esm/utils/schema-converter.test.d.ts +1 -0
- package/dist/esm/utils/tool-input-normalizer.d.ts +8 -0
- package/dist/esm/utils/tool-input-normalizer.js +34 -0
- package/dist/esm/utils/tool-input-normalizer.js.map +1 -0
- package/dist/esm/utils/tool-input-normalizer.test.d.ts +1 -0
- package/dist/esm/vertex/auth.d.ts +29 -0
- package/dist/esm/vertex/auth.js +62 -0
- package/dist/esm/vertex/auth.js.map +1 -0
- package/dist/esm/vertex/index.d.ts +12 -0
- package/dist/esm/vertex/index.js +26 -0
- package/dist/esm/vertex/index.js.map +1 -0
- package/package.json +16 -4
- package/src/adapters/text.ts +40 -20
- package/src/index.ts +6 -1
- package/src/model-meta.ts +70 -0
- package/src/tools/function-tool.test.ts +32 -0
- package/src/tools/function-tool.ts +11 -11
- package/src/utils/client.ts +41 -6
- package/src/utils/schema-converter.test.ts +256 -0
- package/src/utils/schema-converter.ts +251 -88
- package/src/utils/tool-input-normalizer.test.ts +50 -0
- package/src/utils/tool-input-normalizer.ts +42 -0
- package/src/vertex/auth.ts +154 -0
- package/src/vertex/index.ts +50 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { MistralTextAdapter } from '../adapters/text.js';
|
|
2
|
+
import { MistralVertexChatModel } from '../model-meta.js';
|
|
3
|
+
import { MistralVertexConfig } from './auth.js';
|
|
4
|
+
export { MistralVertexAuthError, resolveMistralVertexAccessToken, resolveMistralVertexLocation, resolveMistralVertexModelUrl, resolveMistralVertexProject, type MistralVertexConfig, type VertexAuthClient, } from './auth.js';
|
|
5
|
+
export { MISTRAL_VERTEX_CHAT_MODELS, type MistralVertexChatModel, } from '../model-meta.js';
|
|
6
|
+
/**
|
|
7
|
+
* Creates a Mistral chat adapter that talks to Mistral on Vertex AI.
|
|
8
|
+
*
|
|
9
|
+
* Install `google-auth-library` next to `@tanstack/ai-mistral` for
|
|
10
|
+
* Application Default Credentials. Or pass `authClient` or `getAccessToken`.
|
|
11
|
+
*/
|
|
12
|
+
export declare function mistralVertexText<TModel extends MistralVertexChatModel>(model: TModel, config?: MistralVertexConfig): MistralTextAdapter<TModel>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { MistralTextAdapter } from "../adapters/text.js";
|
|
2
|
+
import { MISTRAL_VERTEX_CHAT_MODELS } from "../model-meta.js";
|
|
3
|
+
import { MistralVertexAuthError, resolveMistralVertexAccessToken, resolveMistralVertexLocation, resolveMistralVertexModelUrl, resolveMistralVertexProject } from "./auth.js";
|
|
4
|
+
//#region src/vertex/index.ts
|
|
5
|
+
/**
|
|
6
|
+
* Creates a Mistral chat adapter that talks to Mistral on Vertex AI.
|
|
7
|
+
*
|
|
8
|
+
* Install `google-auth-library` next to `@tanstack/ai-mistral` for
|
|
9
|
+
* Application Default Credentials. Or pass `authClient` or `getAccessToken`.
|
|
10
|
+
*/
|
|
11
|
+
function mistralVertexText(model, config = {}) {
|
|
12
|
+
const resolveRequestUrl = config.resolveRequestUrl ?? ((stream) => {
|
|
13
|
+
return `${resolveMistralVertexModelUrl(model, config)}:${stream ? "streamRawPredict" : "rawPredict"}`;
|
|
14
|
+
});
|
|
15
|
+
return new MistralTextAdapter({
|
|
16
|
+
apiKey: "vertex",
|
|
17
|
+
getAccessToken: () => resolveMistralVertexAccessToken(config),
|
|
18
|
+
resolveRequestUrl,
|
|
19
|
+
requestModel: model,
|
|
20
|
+
defaultHeaders: config.defaultHeaders
|
|
21
|
+
}, model);
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { MISTRAL_VERTEX_CHAT_MODELS, MistralVertexAuthError, mistralVertexText, resolveMistralVertexAccessToken, resolveMistralVertexLocation, resolveMistralVertexModelUrl, resolveMistralVertexProject };
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../src/vertex/index.ts"],"sourcesContent":["import { MistralTextAdapter } from '../adapters/text'\nimport {\n resolveMistralVertexAccessToken,\n resolveMistralVertexModelUrl,\n} from './auth'\nimport type { MistralVertexChatModel } from '../model-meta'\nimport type { MistralVertexConfig } from './auth'\n\nexport {\n MistralVertexAuthError,\n resolveMistralVertexAccessToken,\n resolveMistralVertexLocation,\n resolveMistralVertexModelUrl,\n resolveMistralVertexProject,\n type MistralVertexConfig,\n type VertexAuthClient,\n} from './auth'\nexport {\n MISTRAL_VERTEX_CHAT_MODELS,\n type MistralVertexChatModel,\n} from '../model-meta'\n\n/**\n * Creates a Mistral chat adapter that talks to Mistral on Vertex AI.\n *\n * Install `google-auth-library` next to `@tanstack/ai-mistral` for\n * Application Default Credentials. Or pass `authClient` or `getAccessToken`.\n */\nexport function mistralVertexText<TModel extends MistralVertexChatModel>(\n model: TModel,\n config: MistralVertexConfig = {},\n): MistralTextAdapter<TModel> {\n const resolveRequestUrl =\n config.resolveRequestUrl ??\n ((stream: boolean) => {\n const modelUrl = resolveMistralVertexModelUrl(model, config)\n return `${modelUrl}:${stream ? 'streamRawPredict' : 'rawPredict'}`\n })\n\n return new MistralTextAdapter(\n {\n apiKey: 'vertex',\n getAccessToken: () => resolveMistralVertexAccessToken(config),\n resolveRequestUrl,\n requestModel: model,\n defaultHeaders: config.defaultHeaders,\n },\n model,\n )\n}\n"],"mappings":";;;;;;;;;;AA4BA,SAAgB,kBACd,OACA,SAA8B,CAAC,GACH;CAC5B,MAAM,oBACJ,OAAO,uBACL,WAAoB;EAEpB,OAAO,GADU,6BAA6B,OAAO,MAC3C,EAAS,GAAG,SAAS,qBAAqB;CACtD;CAEF,OAAO,IAAI,mBACT;EACE,QAAQ;EACR,sBAAsB,gCAAgC,MAAM;EAC5D;EACA,cAAc;EACd,gBAAgB,OAAO;CACzB,GACA,KACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-mistral",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Mistral adapter for TanStack AI",
|
|
6
6
|
"author": "",
|
|
@@ -24,6 +24,10 @@
|
|
|
24
24
|
"./adapters/embedding": {
|
|
25
25
|
"types": "./dist/esm/adapters/embedding.d.ts",
|
|
26
26
|
"import": "./dist/esm/adapters/embedding.js"
|
|
27
|
+
},
|
|
28
|
+
"./vertex": {
|
|
29
|
+
"types": "./dist/esm/vertex/index.d.ts",
|
|
30
|
+
"import": "./dist/esm/vertex/index.js"
|
|
27
31
|
}
|
|
28
32
|
},
|
|
29
33
|
"files": [
|
|
@@ -38,15 +42,23 @@
|
|
|
38
42
|
],
|
|
39
43
|
"devDependencies": {
|
|
40
44
|
"@vitest/coverage-v8": "4.1.10",
|
|
45
|
+
"google-auth-library": "^10.5.0",
|
|
41
46
|
"vite": "^8.2.1",
|
|
42
|
-
"@tanstack/ai": "0.
|
|
47
|
+
"@tanstack/ai": "0.47.3"
|
|
43
48
|
},
|
|
44
49
|
"peerDependencies": {
|
|
50
|
+
"google-auth-library": "^10.5.0",
|
|
45
51
|
"zod": "^4.0.0",
|
|
46
|
-
"@tanstack/ai": "^0.
|
|
52
|
+
"@tanstack/ai": "^0.47.3"
|
|
53
|
+
},
|
|
54
|
+
"peerDependenciesMeta": {
|
|
55
|
+
"google-auth-library": {
|
|
56
|
+
"optional": true
|
|
57
|
+
}
|
|
47
58
|
},
|
|
48
59
|
"dependencies": {
|
|
49
|
-
"@mistralai/mistralai": "2.2.0"
|
|
60
|
+
"@mistralai/mistralai": "2.2.0",
|
|
61
|
+
"@tanstack/ai-utils": "0.4.0"
|
|
50
62
|
},
|
|
51
63
|
"scripts": {
|
|
52
64
|
"build": "vite build",
|
package/src/adapters/text.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { BaseTextAdapter } from '@tanstack/ai/adapters'
|
|
2
|
+
import { undoNullWidening } from '@tanstack/ai-utils'
|
|
2
3
|
import { convertToolsToProviderFormat } from '../tools/tool-converter'
|
|
3
4
|
import {
|
|
4
5
|
createMistralClient,
|
|
5
6
|
generateId,
|
|
6
7
|
getMistralApiKeyFromEnv,
|
|
7
8
|
} from '../utils/client'
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
transformNullsToUndefined,
|
|
11
|
-
} from '../utils/schema-converter'
|
|
9
|
+
import { makeMistralStructuredOutputCompatibleWithMap } from '../utils/schema-converter'
|
|
10
|
+
import { createToolInputNormalizer } from '../utils/tool-input-normalizer'
|
|
12
11
|
import type {
|
|
13
12
|
ContentPart,
|
|
14
13
|
Modality,
|
|
@@ -20,6 +19,7 @@ import type {
|
|
|
20
19
|
MISTRAL_CHAT_MODELS,
|
|
21
20
|
MistralChatModelProviderOptionsByName,
|
|
22
21
|
MistralModelInputModalitiesByName,
|
|
22
|
+
MistralTextAdapterModel,
|
|
23
23
|
} from '../model-meta'
|
|
24
24
|
import type {
|
|
25
25
|
StructuredOutputOptions,
|
|
@@ -38,6 +38,7 @@ import type {
|
|
|
38
38
|
MistralMessageMetadataByModality,
|
|
39
39
|
} from '../message-types'
|
|
40
40
|
import type { MistralClientConfig } from '../utils/client'
|
|
41
|
+
import type { ToolInputNormalizer } from '../utils/tool-input-normalizer'
|
|
41
42
|
|
|
42
43
|
/** Cast an event object to StreamChunk. Adapters construct events with string
|
|
43
44
|
* literal types which are structurally compatible with the EventType enum. */
|
|
@@ -50,14 +51,17 @@ const asChunk = (chunk: Record<string, unknown>) =>
|
|
|
50
51
|
* error if the JSON is malformed — silently substituting `{}` would let a
|
|
51
52
|
* tool fire with empty inputs, masking truncated streams or mis-shaped output.
|
|
52
53
|
*/
|
|
53
|
-
function parseToolCallInput(
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
function parseToolCallInput(
|
|
55
|
+
toolCall: {
|
|
56
|
+
id: string
|
|
57
|
+
name: string
|
|
58
|
+
arguments: string
|
|
59
|
+
},
|
|
60
|
+
normalizeToolInput: ToolInputNormalizer,
|
|
61
|
+
): unknown {
|
|
58
62
|
if (!toolCall.arguments) return {}
|
|
59
63
|
try {
|
|
60
|
-
return
|
|
64
|
+
return normalizeToolInput(toolCall.name, JSON.parse(toolCall.arguments))
|
|
61
65
|
} catch (cause) {
|
|
62
66
|
const preview = toolCall.arguments.slice(0, 200)
|
|
63
67
|
const ellipsis = toolCall.arguments.length > 200 ? '...' : ''
|
|
@@ -160,7 +164,7 @@ interface MistralRawChunk {
|
|
|
160
164
|
* Tree-shakeable adapter for Mistral chat/text completion functionality.
|
|
161
165
|
*/
|
|
162
166
|
export class MistralTextAdapter<
|
|
163
|
-
TModel extends
|
|
167
|
+
TModel extends MistralTextAdapterModel,
|
|
164
168
|
TProviderOptions extends Record<string, any> = ResolveProviderOptions<TModel>,
|
|
165
169
|
TInputModalities extends ReadonlyArray<Modality> =
|
|
166
170
|
ResolveInputModalities<TModel>,
|
|
@@ -242,7 +246,11 @@ export class MistralTextAdapter<
|
|
|
242
246
|
const { stream: _stream, ...nonStreamParams } =
|
|
243
247
|
this.mapTextOptionsToMistral(chatOptions)
|
|
244
248
|
|
|
245
|
-
const
|
|
249
|
+
const {
|
|
250
|
+
schema: jsonSchema,
|
|
251
|
+
nullWideningMap,
|
|
252
|
+
strict,
|
|
253
|
+
} = makeMistralStructuredOutputCompatibleWithMap(
|
|
246
254
|
outputSchema,
|
|
247
255
|
outputSchema.required || [],
|
|
248
256
|
)
|
|
@@ -254,7 +262,7 @@ export class MistralTextAdapter<
|
|
|
254
262
|
jsonSchema: {
|
|
255
263
|
name: 'structured_output',
|
|
256
264
|
schemaDefinition: jsonSchema,
|
|
257
|
-
strict
|
|
265
|
+
strict,
|
|
258
266
|
},
|
|
259
267
|
},
|
|
260
268
|
})
|
|
@@ -273,7 +281,7 @@ export class MistralTextAdapter<
|
|
|
273
281
|
|
|
274
282
|
const usage = response.usage
|
|
275
283
|
return {
|
|
276
|
-
data:
|
|
284
|
+
data: undoNullWidening(parsed, nullWideningMap),
|
|
277
285
|
rawText: textContent,
|
|
278
286
|
...(usage && {
|
|
279
287
|
usage: {
|
|
@@ -306,6 +314,7 @@ export class MistralTextAdapter<
|
|
|
306
314
|
let hasEmittedToolCall = false
|
|
307
315
|
let hasEmittedRunFinished = false
|
|
308
316
|
let lastChunkModel = options.model
|
|
317
|
+
const normalizeToolInput = createToolInputNormalizer(options.tools)
|
|
309
318
|
|
|
310
319
|
// Reasoning lifecycle (magistral-* models stream `thinking` content
|
|
311
320
|
// parts before any text). Mirrors the anthropic adapter's pattern:
|
|
@@ -513,7 +522,10 @@ export class MistralTextAdapter<
|
|
|
513
522
|
continue
|
|
514
523
|
}
|
|
515
524
|
|
|
516
|
-
const parsedInput = parseToolCallInput(
|
|
525
|
+
const parsedInput = parseToolCallInput(
|
|
526
|
+
toolCall,
|
|
527
|
+
normalizeToolInput,
|
|
528
|
+
)
|
|
517
529
|
|
|
518
530
|
toolCall.ended = true
|
|
519
531
|
hasEmittedToolCall = true
|
|
@@ -615,7 +627,7 @@ export class MistralTextAdapter<
|
|
|
615
627
|
toolName: toolCall.name,
|
|
616
628
|
model: lastChunkModel,
|
|
617
629
|
timestamp,
|
|
618
|
-
input: parseToolCallInput(toolCall),
|
|
630
|
+
input: parseToolCallInput(toolCall, normalizeToolInput),
|
|
619
631
|
})
|
|
620
632
|
}
|
|
621
633
|
}
|
|
@@ -676,7 +688,10 @@ export class MistralTextAdapter<
|
|
|
676
688
|
let partialInput: unknown = {}
|
|
677
689
|
try {
|
|
678
690
|
partialInput = toolCall.arguments
|
|
679
|
-
?
|
|
691
|
+
? normalizeToolInput(
|
|
692
|
+
toolCall.name,
|
|
693
|
+
JSON.parse(toolCall.arguments),
|
|
694
|
+
)
|
|
680
695
|
: {}
|
|
681
696
|
} catch {
|
|
682
697
|
partialInput = {}
|
|
@@ -708,13 +723,18 @@ export class MistralTextAdapter<
|
|
|
708
723
|
const serverURL = (config.serverURL ?? 'https://api.mistral.ai')
|
|
709
724
|
.replace(/\/+$/, '')
|
|
710
725
|
.replace(/\/v1$/, '')
|
|
711
|
-
const url =
|
|
726
|
+
const url =
|
|
727
|
+
config.resolveRequestUrl?.(true) ?? `${serverURL}/v1/chat/completions`
|
|
712
728
|
|
|
713
729
|
const body = this.toWireBody(params)
|
|
730
|
+
const accessToken =
|
|
731
|
+
config.getAccessToken === undefined
|
|
732
|
+
? config.apiKey
|
|
733
|
+
: await config.getAccessToken()
|
|
714
734
|
|
|
715
735
|
const headers: Record<string, string> = {
|
|
716
736
|
'Content-Type': 'application/json',
|
|
717
|
-
Authorization: `Bearer ${
|
|
737
|
+
Authorization: `Bearer ${accessToken}`,
|
|
718
738
|
...config.defaultHeaders,
|
|
719
739
|
}
|
|
720
740
|
|
|
@@ -906,7 +926,7 @@ export class MistralTextAdapter<
|
|
|
906
926
|
}
|
|
907
927
|
|
|
908
928
|
return {
|
|
909
|
-
model: options.model,
|
|
929
|
+
model: this.rawConfig.requestModel ?? options.model,
|
|
910
930
|
messages: messages,
|
|
911
931
|
temperature: modelOptions?.temperature ?? undefined,
|
|
912
932
|
maxTokens: modelOptions?.max_tokens ?? undefined,
|
package/src/index.ts
CHANGED
|
@@ -34,11 +34,16 @@ export type {
|
|
|
34
34
|
ResolveProviderOptions,
|
|
35
35
|
ResolveInputModalities,
|
|
36
36
|
MistralChatModels,
|
|
37
|
+
MistralVertexChatModel,
|
|
37
38
|
MistralEmbeddingModel,
|
|
38
39
|
MistralEmbeddingModelProviderOptionsByName,
|
|
39
40
|
MistralEmbeddingModelInputModalitiesByName,
|
|
40
41
|
} from './model-meta'
|
|
41
|
-
export {
|
|
42
|
+
export {
|
|
43
|
+
MISTRAL_CHAT_MODELS,
|
|
44
|
+
MISTRAL_EMBEDDING_MODELS,
|
|
45
|
+
MISTRAL_VERTEX_CHAT_MODELS,
|
|
46
|
+
} from './model-meta'
|
|
42
47
|
export type {
|
|
43
48
|
MistralTextMetadata,
|
|
44
49
|
MistralImageMetadata,
|
package/src/model-meta.ts
CHANGED
|
@@ -216,6 +216,56 @@ const OPEN_MISTRAL_NEMO = {
|
|
|
216
216
|
},
|
|
217
217
|
} as const satisfies ModelMeta<MistralTextProviderOptions>
|
|
218
218
|
|
|
219
|
+
// Vertex Model Garden IDs. These are not the Mistral API catalog.
|
|
220
|
+
// Source: https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/partner-models/mistral
|
|
221
|
+
const MISTRAL_MEDIUM_3 = {
|
|
222
|
+
name: 'mistral-medium-3',
|
|
223
|
+
context_window: 131_072,
|
|
224
|
+
max_completion_tokens: 8_192,
|
|
225
|
+
pricing: {
|
|
226
|
+
input: { normal: 0.4 },
|
|
227
|
+
output: { normal: 2 },
|
|
228
|
+
},
|
|
229
|
+
supports: {
|
|
230
|
+
input: ['text', 'image', 'document'],
|
|
231
|
+
output: ['text'],
|
|
232
|
+
endpoints: ['chat'],
|
|
233
|
+
features: ['streaming', 'tools', 'json_object', 'json_schema', 'vision'],
|
|
234
|
+
},
|
|
235
|
+
} as const satisfies ModelMeta<MistralTextProviderOptions>
|
|
236
|
+
|
|
237
|
+
const MISTRAL_SMALL_2503 = {
|
|
238
|
+
name: 'mistral-small-2503',
|
|
239
|
+
context_window: 131_072,
|
|
240
|
+
max_completion_tokens: 8_192,
|
|
241
|
+
pricing: {
|
|
242
|
+
input: { normal: 0.1 },
|
|
243
|
+
output: { normal: 0.3 },
|
|
244
|
+
},
|
|
245
|
+
supports: {
|
|
246
|
+
input: ['text', 'image', 'document'],
|
|
247
|
+
output: ['text'],
|
|
248
|
+
endpoints: ['chat'],
|
|
249
|
+
features: ['streaming', 'tools', 'json_object', 'json_schema', 'vision'],
|
|
250
|
+
},
|
|
251
|
+
} as const satisfies ModelMeta<MistralTextProviderOptions>
|
|
252
|
+
|
|
253
|
+
const CODESTRAL_2 = {
|
|
254
|
+
name: 'codestral-2',
|
|
255
|
+
context_window: 131_072,
|
|
256
|
+
max_completion_tokens: 8_192,
|
|
257
|
+
pricing: {
|
|
258
|
+
input: { normal: 0.3 },
|
|
259
|
+
output: { normal: 0.9 },
|
|
260
|
+
},
|
|
261
|
+
supports: {
|
|
262
|
+
input: ['text'],
|
|
263
|
+
output: ['text'],
|
|
264
|
+
endpoints: ['chat'],
|
|
265
|
+
features: ['streaming', 'tools', 'json_object', 'json_schema', 'code'],
|
|
266
|
+
},
|
|
267
|
+
} as const satisfies ModelMeta<MistralTextProviderOptions>
|
|
268
|
+
|
|
219
269
|
/**
|
|
220
270
|
* All supported Mistral chat model identifiers.
|
|
221
271
|
*/
|
|
@@ -238,6 +288,20 @@ export const MISTRAL_CHAT_MODELS = [
|
|
|
238
288
|
*/
|
|
239
289
|
export type MistralChatModels = (typeof MISTRAL_CHAT_MODELS)[number]
|
|
240
290
|
|
|
291
|
+
/**
|
|
292
|
+
* Mistral chat models on Vertex AI / Gemini Enterprise Agent Platform.
|
|
293
|
+
* This list is the Google partner catalog, not the Mistral API catalog.
|
|
294
|
+
* OCR (`mistral-ocr-2505`) is not a chat model.
|
|
295
|
+
*/
|
|
296
|
+
export const MISTRAL_VERTEX_CHAT_MODELS = [
|
|
297
|
+
MISTRAL_MEDIUM_3.name,
|
|
298
|
+
MISTRAL_SMALL_2503.name,
|
|
299
|
+
CODESTRAL_2.name,
|
|
300
|
+
] as const
|
|
301
|
+
|
|
302
|
+
export type MistralVertexChatModel = (typeof MISTRAL_VERTEX_CHAT_MODELS)[number]
|
|
303
|
+
export type MistralTextAdapterModel = MistralChatModels | MistralVertexChatModel
|
|
304
|
+
|
|
241
305
|
/**
|
|
242
306
|
* Type-only map from Mistral chat model name to its supported input modalities.
|
|
243
307
|
*/
|
|
@@ -253,6 +317,9 @@ export type MistralModelInputModalitiesByName = {
|
|
|
253
317
|
[MAGISTRAL_MEDIUM_LATEST.name]: typeof MAGISTRAL_MEDIUM_LATEST.supports.input
|
|
254
318
|
[MAGISTRAL_SMALL_LATEST.name]: typeof MAGISTRAL_SMALL_LATEST.supports.input
|
|
255
319
|
[OPEN_MISTRAL_NEMO.name]: typeof OPEN_MISTRAL_NEMO.supports.input
|
|
320
|
+
[MISTRAL_MEDIUM_3.name]: typeof MISTRAL_MEDIUM_3.supports.input
|
|
321
|
+
[MISTRAL_SMALL_2503.name]: typeof MISTRAL_SMALL_2503.supports.input
|
|
322
|
+
[CODESTRAL_2.name]: typeof CODESTRAL_2.supports.input
|
|
256
323
|
}
|
|
257
324
|
|
|
258
325
|
/**
|
|
@@ -270,6 +337,9 @@ export type MistralChatModelProviderOptionsByName = {
|
|
|
270
337
|
[MAGISTRAL_MEDIUM_LATEST.name]: MistralReasoningProviderOptions
|
|
271
338
|
[MAGISTRAL_SMALL_LATEST.name]: MistralReasoningProviderOptions
|
|
272
339
|
[OPEN_MISTRAL_NEMO.name]: MistralTextProviderOptions
|
|
340
|
+
[MISTRAL_MEDIUM_3.name]: MistralVisionProviderOptions
|
|
341
|
+
[MISTRAL_SMALL_2503.name]: MistralVisionProviderOptions
|
|
342
|
+
[CODESTRAL_2.name]: MistralTextProviderOptions
|
|
273
343
|
}
|
|
274
344
|
|
|
275
345
|
/**
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { convertFunctionToolToAdapterFormat } from './function-tool'
|
|
3
|
+
|
|
4
|
+
describe('convertFunctionToolToAdapterFormat', () => {
|
|
5
|
+
it('preserves composed schemas under the non-strict fallback', () => {
|
|
6
|
+
const inputSchema = {
|
|
7
|
+
type: 'object',
|
|
8
|
+
properties: {
|
|
9
|
+
value: {
|
|
10
|
+
oneOf: [{ type: 'string' }, { type: 'number' }],
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
required: [],
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
expect(
|
|
17
|
+
convertFunctionToolToAdapterFormat({
|
|
18
|
+
name: 'store_value',
|
|
19
|
+
description: 'Store a value',
|
|
20
|
+
inputSchema,
|
|
21
|
+
}),
|
|
22
|
+
).toEqual({
|
|
23
|
+
type: 'function',
|
|
24
|
+
function: {
|
|
25
|
+
name: 'store_value',
|
|
26
|
+
description: 'Store a value',
|
|
27
|
+
parameters: inputSchema,
|
|
28
|
+
strict: false,
|
|
29
|
+
},
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
})
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { makeMistralStructuredOutputCompatibleWithMap } from '../utils/schema-converter'
|
|
2
2
|
import type { JSONSchema, Tool } from '@tanstack/ai'
|
|
3
3
|
import type { ChatCompletionTool } from '../message-types'
|
|
4
4
|
|
|
@@ -7,11 +7,10 @@ export type FunctionTool = ChatCompletionTool
|
|
|
7
7
|
/**
|
|
8
8
|
* Converts a standard Tool to Mistral ChatCompletionTool format.
|
|
9
9
|
*
|
|
10
|
-
* Tool schemas are already
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* - additionalProperties: false
|
|
10
|
+
* Tool schemas are already JSON Schema in the ai layer. When the schema can
|
|
11
|
+
* be inverted, rewrite it for strict mode (required, nullable optionals,
|
|
12
|
+
* `additionalProperties: false`) and set `strict: true`. Otherwise leave the
|
|
13
|
+
* schema intact and set `strict: false`.
|
|
15
14
|
*/
|
|
16
15
|
export function convertFunctionToolToAdapterFormat(tool: Tool): FunctionTool {
|
|
17
16
|
const baseSchema = (tool.inputSchema ?? {
|
|
@@ -25,10 +24,11 @@ export function convertFunctionToolToAdapterFormat(tool: Tool): FunctionTool {
|
|
|
25
24
|
? { ...baseSchema, properties: {} }
|
|
26
25
|
: { ...baseSchema }
|
|
27
26
|
|
|
28
|
-
const jsonSchema =
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
const { schema: jsonSchema, strict } =
|
|
28
|
+
makeMistralStructuredOutputCompatibleWithMap(
|
|
29
|
+
inputSchema,
|
|
30
|
+
inputSchema.required || [],
|
|
31
|
+
)
|
|
32
32
|
|
|
33
33
|
return {
|
|
34
34
|
type: 'function',
|
|
@@ -36,7 +36,7 @@ export function convertFunctionToolToAdapterFormat(tool: Tool): FunctionTool {
|
|
|
36
36
|
name: tool.name,
|
|
37
37
|
description: tool.description,
|
|
38
38
|
parameters: jsonSchema,
|
|
39
|
-
strict
|
|
39
|
+
strict,
|
|
40
40
|
},
|
|
41
41
|
} satisfies FunctionTool
|
|
42
42
|
}
|
package/src/utils/client.ts
CHANGED
|
@@ -12,22 +12,57 @@ export interface MistralClientConfig {
|
|
|
12
12
|
|
|
13
13
|
/** Optional default headers to include with every request. */
|
|
14
14
|
defaultHeaders?: Record<string, string>
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Optional Google / Vertex access token. When set, it replaces
|
|
18
|
+
* `apiKey` on the Authorization header.
|
|
19
|
+
*/
|
|
20
|
+
getAccessToken?: () => Promise<string>
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Optional chat completions URL. Vertex uses this for
|
|
24
|
+
* `:rawPredict` and `:streamRawPredict`.
|
|
25
|
+
*/
|
|
26
|
+
resolveRequestUrl?: (stream: boolean) => string
|
|
27
|
+
|
|
28
|
+
/** Optional model id sent on the wire. Vertex uses publisher model ids. */
|
|
29
|
+
requestModel?: string
|
|
15
30
|
}
|
|
16
31
|
|
|
17
32
|
/**
|
|
18
33
|
* Creates a Mistral SDK client instance.
|
|
19
34
|
*/
|
|
20
35
|
export function createMistralClient(config: MistralClientConfig): Mistral {
|
|
21
|
-
const {
|
|
36
|
+
const {
|
|
37
|
+
apiKey,
|
|
38
|
+
serverURL,
|
|
39
|
+
timeoutMs,
|
|
40
|
+
defaultHeaders,
|
|
41
|
+
getAccessToken,
|
|
42
|
+
resolveRequestUrl,
|
|
43
|
+
} = config
|
|
44
|
+
|
|
45
|
+
const needsHook =
|
|
46
|
+
(defaultHeaders !== undefined && Object.keys(defaultHeaders).length > 0) ||
|
|
47
|
+
getAccessToken !== undefined ||
|
|
48
|
+
resolveRequestUrl !== undefined
|
|
22
49
|
|
|
23
50
|
let httpClient: HTTPClient | undefined
|
|
24
|
-
if (
|
|
51
|
+
if (needsHook) {
|
|
25
52
|
httpClient = new HTTPClient()
|
|
26
|
-
httpClient.addHook('beforeRequest', (req) => {
|
|
27
|
-
|
|
28
|
-
req.
|
|
53
|
+
httpClient.addHook('beforeRequest', async (req) => {
|
|
54
|
+
const nextUrl =
|
|
55
|
+
resolveRequestUrl === undefined ? req.url : resolveRequestUrl(false)
|
|
56
|
+
const next = new Request(nextUrl, req)
|
|
57
|
+
if (defaultHeaders) {
|
|
58
|
+
for (const [key, value] of Object.entries(defaultHeaders)) {
|
|
59
|
+
next.headers.set(key, value)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (getAccessToken !== undefined) {
|
|
63
|
+
next.headers.set('Authorization', `Bearer ${await getAccessToken()}`)
|
|
29
64
|
}
|
|
30
|
-
return
|
|
65
|
+
return next
|
|
31
66
|
})
|
|
32
67
|
}
|
|
33
68
|
|