@tanstack/ai-mistral 1.0.0 → 1.1.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.
- package/README.md +4 -3
- package/dist/esm/adapters/embedding.d.ts +2 -2
- package/dist/esm/adapters/embedding.js +3 -2
- package/dist/esm/adapters/embedding.js.map +1 -1
- package/dist/esm/adapters/text.js +52 -32
- package/dist/esm/adapters/text.js.map +1 -1
- package/dist/esm/client/index.d.ts +181 -0
- package/dist/esm/client/index.js +385 -0
- package/dist/esm/client/index.js.map +1 -0
- package/dist/esm/utils/client.d.ts +0 -5
- package/dist/esm/utils/client.js +1 -33
- package/dist/esm/utils/client.js.map +1 -1
- package/package.json +5 -4
- package/src/adapters/embedding.ts +9 -8
- package/src/adapters/text.ts +102 -48
- package/src/client/index.ts +588 -0
- package/src/utils/client.ts +0 -67
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","names":[],"sources":["../../../src/utils/client.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"client.js","names":[],"sources":["../../../src/utils/client.ts"],"sourcesContent":["export interface MistralClientConfig {\n /** Mistral API key. */\n apiKey: string\n\n /**\n * Base URL for every request. Same option name as the other adapters, so a\n * gateway config can be spread into any of them. Wins over `serverURL` when\n * both are set.\n */\n baseURL?: string\n\n /** Alias of `baseURL`. */\n serverURL?: string\n\n /** Optional request timeout (ms). */\n timeoutMs?: number\n\n /** Optional default headers to include with every request. */\n defaultHeaders?: Record<string, string>\n\n /**\n * Optional Google / Vertex access token. When set, it replaces\n * `apiKey` on the Authorization header.\n */\n getAccessToken?: () => Promise<string>\n\n /**\n * Optional chat completions URL. Vertex uses this for\n * `:rawPredict` and `:streamRawPredict`.\n */\n resolveRequestUrl?: (stream: boolean) => string\n\n /** Optional model id sent on the wire. Vertex uses publisher model ids. */\n requestModel?: string\n}\n\n/**\n * Gets Mistral API key from environment variables.\n * @throws Error if MISTRAL_API_KEY is not found\n */\nexport function getMistralApiKeyFromEnv(): string {\n let key: string | undefined\n\n if (typeof process !== 'undefined' && typeof process.env !== 'undefined') {\n key = process.env.MISTRAL_API_KEY\n } else {\n const g = globalThis as { window?: { env?: Record<string, string> } }\n key = g.window?.env?.MISTRAL_API_KEY\n }\n\n if (!key) {\n throw new Error(\n 'MISTRAL_API_KEY is required. In Node.js set it as an environment variable; in browser environments inject it via window.env.MISTRAL_API_KEY or use the factory function with an explicit API key.',\n )\n }\n\n return key\n}\n\n/**\n * Generates a unique ID with a prefix.\n */\nexport function generateId(prefix: string): string {\n return `${prefix}-${crypto.randomUUID()}`\n}\n"],"mappings":";;;;;AAwCA,SAAgB,0BAAkC;CAChD,IAAI;CAEJ,IAAI,OAAO,YAAY,eAAe,OAAO,QAAQ,QAAQ,aAC3D,MAAM,QAAQ,IAAI;MAGlB,MAAM,WAAE,QAAQ,KAAK;CAGvB,IAAI,CAAC,KACH,MAAM,IAAI,MACR,mMACF;CAGF,OAAO;AACT;;;;AAKA,SAAgB,WAAW,QAAwB;CACjD,OAAO,GAAG,OAAO,GAAG,OAAO,WAAW;AACxC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-mistral",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Mistral adapter for TanStack AI",
|
|
6
6
|
"author": "",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"import": "./dist/esm/vertex/index.js"
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
|
+
"sideEffects": false,
|
|
37
38
|
"files": [
|
|
38
39
|
"dist",
|
|
39
40
|
"src"
|
|
@@ -48,11 +49,11 @@
|
|
|
48
49
|
"@vitest/coverage-v8": "4.1.10",
|
|
49
50
|
"google-auth-library": "^10.5.0",
|
|
50
51
|
"vite": "^8.2.1",
|
|
51
|
-
"@tanstack/ai": "1.
|
|
52
|
+
"@tanstack/ai": "1.2.0"
|
|
52
53
|
},
|
|
53
54
|
"peerDependencies": {
|
|
54
55
|
"google-auth-library": "^10.5.0",
|
|
55
|
-
"@tanstack/ai": "^1.
|
|
56
|
+
"@tanstack/ai": "^1.2.0"
|
|
56
57
|
},
|
|
57
58
|
"peerDependenciesMeta": {
|
|
58
59
|
"google-auth-library": {
|
|
@@ -61,7 +62,7 @@
|
|
|
61
62
|
},
|
|
62
63
|
"dependencies": {
|
|
63
64
|
"@mistralai/mistralai": "2.2.0",
|
|
64
|
-
"@tanstack/ai-utils": "^1.
|
|
65
|
+
"@tanstack/ai-utils": "^1.1.0"
|
|
65
66
|
},
|
|
66
67
|
"scripts": {
|
|
67
68
|
"build": "vite build",
|
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
import { BaseEmbeddingAdapter } from '@tanstack/ai/adapters'
|
|
2
2
|
import { toRunErrorPayload } from '@tanstack/ai/adapter-internals'
|
|
3
3
|
import { requireTextOnlyEmbeddingInput } from '@tanstack/ai'
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
generateId,
|
|
7
|
-
getMistralApiKeyFromEnv,
|
|
8
|
-
} from '../utils/client'
|
|
4
|
+
import { createMistralClient } from '../client/index'
|
|
5
|
+
import { generateId, getMistralApiKeyFromEnv } from '../utils/client'
|
|
9
6
|
import type {
|
|
10
7
|
EmbeddingOptions,
|
|
11
8
|
EmbeddingResult,
|
|
12
9
|
TokenUsage,
|
|
13
10
|
} from '@tanstack/ai'
|
|
14
|
-
import type {
|
|
11
|
+
import type { MistralClient } from '../client/index'
|
|
15
12
|
import type { EmbeddingRequest } from '@mistralai/mistralai/models/components'
|
|
16
13
|
import type {
|
|
17
14
|
MistralEmbeddingModel,
|
|
@@ -48,7 +45,7 @@ export class MistralEmbeddingAdapter<
|
|
|
48
45
|
> {
|
|
49
46
|
readonly name = 'mistral' as const
|
|
50
47
|
|
|
51
|
-
protected client:
|
|
48
|
+
protected client: MistralClient
|
|
52
49
|
|
|
53
50
|
constructor(config: MistralEmbeddingConfig, model: TModel) {
|
|
54
51
|
super(model, {})
|
|
@@ -85,7 +82,11 @@ export class MistralEmbeddingAdapter<
|
|
|
85
82
|
{ provider: this.name, model },
|
|
86
83
|
)
|
|
87
84
|
|
|
88
|
-
const response =
|
|
85
|
+
const response = options.abortSignal
|
|
86
|
+
? await this.client.embeddings.create(request, {
|
|
87
|
+
fetchOptions: { signal: options.abortSignal },
|
|
88
|
+
})
|
|
89
|
+
: await this.client.embeddings.create(request)
|
|
89
90
|
|
|
90
91
|
const usage: TokenUsage = {
|
|
91
92
|
promptTokens: response.usage.promptTokens ?? 0,
|
package/src/adapters/text.ts
CHANGED
|
@@ -2,11 +2,8 @@ import { isFileSource, unsupportedFileSourceError } from '@tanstack/ai'
|
|
|
2
2
|
import { BaseTextAdapter } from '@tanstack/ai/adapters'
|
|
3
3
|
import { undoNullWidening } from '@tanstack/ai-utils'
|
|
4
4
|
import { convertToolsToProviderFormat } from '../tools/tool-converter'
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
generateId,
|
|
8
|
-
getMistralApiKeyFromEnv,
|
|
9
|
-
} from '../utils/client'
|
|
5
|
+
import { createMistralClient } from '../client/index'
|
|
6
|
+
import { generateId, getMistralApiKeyFromEnv } from '../utils/client'
|
|
10
7
|
import { makeMistralStructuredOutputCompatibleWithMap } from '../utils/schema-converter'
|
|
11
8
|
import { createToolInputNormalizer } from '../utils/tool-input-normalizer'
|
|
12
9
|
import {
|
|
@@ -29,6 +26,7 @@ import type {
|
|
|
29
26
|
ReasoningRequest,
|
|
30
27
|
ResolvedPromptCache,
|
|
31
28
|
TextOptions,
|
|
29
|
+
TokenUsage,
|
|
32
30
|
ToolChoice,
|
|
33
31
|
} from '@tanstack/ai'
|
|
34
32
|
import type {
|
|
@@ -42,7 +40,6 @@ import type {
|
|
|
42
40
|
StructuredOutputOptions,
|
|
43
41
|
StructuredOutputResult,
|
|
44
42
|
} from '@tanstack/ai/adapters'
|
|
45
|
-
import type { Mistral } from '@mistralai/mistralai'
|
|
46
43
|
import type {
|
|
47
44
|
AssistantMessage,
|
|
48
45
|
ChatCompletionStreamRequest,
|
|
@@ -58,6 +55,7 @@ import type {
|
|
|
58
55
|
MistralImageMetadata,
|
|
59
56
|
MistralMessageMetadataByModality,
|
|
60
57
|
} from '../message-types'
|
|
58
|
+
import type { MistralClient } from '../client/index'
|
|
61
59
|
import type { MistralClientConfig } from '../utils/client'
|
|
62
60
|
import type { ToolInputNormalizer } from '../utils/tool-input-normalizer'
|
|
63
61
|
|
|
@@ -314,14 +312,14 @@ export class MistralTextAdapter<
|
|
|
314
312
|
override readonly provider: string
|
|
315
313
|
override readonly inputModalities = MISTRAL_MODEL_INPUT_MODALITIES[this.model]
|
|
316
314
|
|
|
317
|
-
private readonly client:
|
|
315
|
+
private readonly client: MistralClient
|
|
318
316
|
private readonly rawConfig: MistralClientConfig
|
|
319
317
|
|
|
320
318
|
constructor(config: MistralTextConfig, model: TModel) {
|
|
321
319
|
super(config, model)
|
|
322
|
-
// The
|
|
323
|
-
//
|
|
324
|
-
//
|
|
320
|
+
// The client serves `structuredOutput` (non-streaming). The streaming
|
|
321
|
+
// path uses `fetchRawMistralStream`, which reads partial tool-call
|
|
322
|
+
// argument deltas as they come.
|
|
325
323
|
this.client = createMistralClient(config)
|
|
326
324
|
this.rawConfig = config
|
|
327
325
|
this.provider =
|
|
@@ -336,7 +334,14 @@ export class MistralTextAdapter<
|
|
|
336
334
|
const requestParams = this.mapTextOptionsToMistral(options)
|
|
337
335
|
const timestamp = Date.now()
|
|
338
336
|
|
|
339
|
-
const aguiState
|
|
337
|
+
const aguiState: {
|
|
338
|
+
runId: string
|
|
339
|
+
threadId: string
|
|
340
|
+
messageId: string
|
|
341
|
+
timestamp: number
|
|
342
|
+
hasEmittedRunStarted: boolean
|
|
343
|
+
usage?: TokenUsage
|
|
344
|
+
} = {
|
|
340
345
|
runId: options.runId ?? generateId(this.name),
|
|
341
346
|
threadId: options.threadId,
|
|
342
347
|
messageId: generateId(this.name),
|
|
@@ -356,6 +361,7 @@ export class MistralTextAdapter<
|
|
|
356
361
|
body,
|
|
357
362
|
this.rawConfig,
|
|
358
363
|
options.wrapFetch,
|
|
364
|
+
options.request?.signal ?? undefined,
|
|
359
365
|
)
|
|
360
366
|
for await (const chunk of this.processMistralStreamChunks(
|
|
361
367
|
stream,
|
|
@@ -422,6 +428,7 @@ export class MistralTextAdapter<
|
|
|
422
428
|
message: err.message || 'Unknown error',
|
|
423
429
|
code: err.code,
|
|
424
430
|
},
|
|
431
|
+
...(aguiState.usage && { usage: aguiState.usage }),
|
|
425
432
|
})
|
|
426
433
|
|
|
427
434
|
throw err
|
|
@@ -456,23 +463,46 @@ export class MistralTextAdapter<
|
|
|
456
463
|
: chatOptions.wrapFetch
|
|
457
464
|
? createMistralClient(this.rawConfig, next)
|
|
458
465
|
: this.client
|
|
459
|
-
|
|
466
|
+
// The SDK drops its default 30 s timeout when the request has a signal
|
|
467
|
+
// (lib/sdks.js), so keep that timeout next to the caller signal.
|
|
468
|
+
const signal = chatOptions.request?.signal
|
|
469
|
+
const timeoutMs = this.rawConfig.timeoutMs || 30_000
|
|
470
|
+
const params = {
|
|
460
471
|
...nonStreamParams,
|
|
461
472
|
responseFormat: {
|
|
462
|
-
type: 'json_schema',
|
|
473
|
+
type: 'json_schema' as const,
|
|
463
474
|
jsonSchema: {
|
|
464
475
|
name: 'structured_output',
|
|
465
476
|
schemaDefinition: jsonSchema,
|
|
466
477
|
strict,
|
|
467
478
|
},
|
|
468
479
|
},
|
|
469
|
-
}
|
|
480
|
+
}
|
|
481
|
+
const response = signal
|
|
482
|
+
? await client.chat.complete(params, {
|
|
483
|
+
signal:
|
|
484
|
+
timeoutMs > 0
|
|
485
|
+
? AbortSignal.any([signal, AbortSignal.timeout(timeoutMs)])
|
|
486
|
+
: signal,
|
|
487
|
+
})
|
|
488
|
+
: await client.chat.complete(params)
|
|
489
|
+
|
|
490
|
+
const usage = response.usage && {
|
|
491
|
+
promptTokens: response.usage.promptTokens ?? 0,
|
|
492
|
+
completionTokens: response.usage.completionTokens ?? 0,
|
|
493
|
+
totalTokens: response.usage.totalTokens ?? 0,
|
|
494
|
+
...mistralCachedTokens(response.usage),
|
|
495
|
+
}
|
|
470
496
|
|
|
471
497
|
// A response cut off at the output cap is a truncated JSON document;
|
|
472
|
-
// report it before the parse error (issue #1426).
|
|
498
|
+
// report it before the parse error (issue #1426). The errors carry the
|
|
499
|
+
// usage, because the provider already billed the tokens.
|
|
473
500
|
if (response.choices[0]?.finishReason === 'length') {
|
|
474
|
-
throw
|
|
475
|
-
|
|
501
|
+
throw Object.assign(
|
|
502
|
+
new Error(
|
|
503
|
+
'mistral.structuredOutput: the response was cut off because the maximum token limit was reached (finish_reason=length); raise modelOptions.max_tokens',
|
|
504
|
+
),
|
|
505
|
+
usage && { usage },
|
|
476
506
|
)
|
|
477
507
|
}
|
|
478
508
|
|
|
@@ -483,25 +513,20 @@ export class MistralTextAdapter<
|
|
|
483
513
|
try {
|
|
484
514
|
parsed = JSON.parse(textContent)
|
|
485
515
|
} catch {
|
|
486
|
-
throw
|
|
487
|
-
|
|
516
|
+
throw Object.assign(
|
|
517
|
+
new Error(
|
|
518
|
+
`Failed to parse structured output as JSON. Content: ${textContent.slice(0, 200)}${textContent.length > 200 ? '...' : ''}`,
|
|
519
|
+
),
|
|
520
|
+
usage && { usage },
|
|
488
521
|
)
|
|
489
522
|
}
|
|
490
523
|
|
|
491
|
-
const usage = response.usage
|
|
492
524
|
return {
|
|
493
525
|
data: undoNullWidening(parsed, nullWideningMap),
|
|
494
526
|
...(response.id && { responseId: response.id }),
|
|
495
527
|
...(response.model && { model: response.model }),
|
|
496
528
|
rawText: textContent,
|
|
497
|
-
...(usage && {
|
|
498
|
-
usage: {
|
|
499
|
-
promptTokens: usage.promptTokens ?? 0,
|
|
500
|
-
completionTokens: usage.completionTokens ?? 0,
|
|
501
|
-
totalTokens: usage.totalTokens ?? 0,
|
|
502
|
-
...mistralCachedTokens(usage),
|
|
503
|
-
},
|
|
504
|
-
}),
|
|
529
|
+
...(usage && { usage }),
|
|
505
530
|
}
|
|
506
531
|
}
|
|
507
532
|
|
|
@@ -517,9 +542,15 @@ export class MistralTextAdapter<
|
|
|
517
542
|
messageId: string
|
|
518
543
|
timestamp: number
|
|
519
544
|
hasEmittedRunStarted: boolean
|
|
545
|
+
usage?: TokenUsage
|
|
520
546
|
},
|
|
521
547
|
): AsyncIterable<AdapterYieldChunk> {
|
|
522
548
|
let accumulatedContent = ''
|
|
549
|
+
// RUN_FINISHED waits for the end of the stream, so usage from a
|
|
550
|
+
// trailing usage-only chunk (`choices: []`) is included.
|
|
551
|
+
let finished:
|
|
552
|
+
| { model: string; finishReason: 'tool_calls' | 'length' | 'stop' }
|
|
553
|
+
| undefined
|
|
523
554
|
const timestamp = aguiState.timestamp
|
|
524
555
|
let hasEmittedTextMessageStart = false
|
|
525
556
|
let hasEmittedTextMessageEnd = false
|
|
@@ -551,6 +582,14 @@ export class MistralTextAdapter<
|
|
|
551
582
|
for await (const chunk of stream) {
|
|
552
583
|
lastChunkModel = chunk.model || lastChunkModel
|
|
553
584
|
if (chunk.id) responseId = chunk.id
|
|
585
|
+
if (chunk.usage) {
|
|
586
|
+
aguiState.usage = {
|
|
587
|
+
promptTokens: chunk.usage.prompt_tokens || 0,
|
|
588
|
+
completionTokens: chunk.usage.completion_tokens || 0,
|
|
589
|
+
totalTokens: chunk.usage.total_tokens || 0,
|
|
590
|
+
...mistralCachedTokens(chunk.usage),
|
|
591
|
+
}
|
|
592
|
+
}
|
|
554
593
|
const choice = chunk.choices?.[0]
|
|
555
594
|
if (!choice) continue
|
|
556
595
|
|
|
@@ -656,7 +695,24 @@ export class MistralTextAdapter<
|
|
|
656
695
|
|
|
657
696
|
if (deltaToolCalls) {
|
|
658
697
|
for (const [i, toolCallDelta] of deltaToolCalls.entries()) {
|
|
659
|
-
|
|
698
|
+
let index = toolCallDelta.index ?? i
|
|
699
|
+
// Mistral can send each parallel call whole with the same `index`
|
|
700
|
+
// (the SDK fills a missing one with 0), so an id that differs from
|
|
701
|
+
// the one already in the slot starts a new call. A repeated id goes
|
|
702
|
+
// back to its own slot.
|
|
703
|
+
if (toolCallDelta.id) {
|
|
704
|
+
const occupant = toolCallsInProgress.get(index)
|
|
705
|
+
if (occupant?.id && occupant.id !== toolCallDelta.id) {
|
|
706
|
+
const sameId = [...toolCallsInProgress].find(
|
|
707
|
+
([, call]) => call.id === toolCallDelta.id,
|
|
708
|
+
)
|
|
709
|
+
if (sameId) {
|
|
710
|
+
index = sameId[0]
|
|
711
|
+
} else {
|
|
712
|
+
while (toolCallsInProgress.has(index)) index += 1
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
}
|
|
660
716
|
|
|
661
717
|
let toolCall = toolCallsInProgress.get(index)
|
|
662
718
|
if (!toolCall) {
|
|
@@ -790,28 +846,24 @@ export class MistralTextAdapter<
|
|
|
790
846
|
})
|
|
791
847
|
}
|
|
792
848
|
|
|
793
|
-
const usage = chunk.usage
|
|
794
849
|
hasEmittedRunFinished = true
|
|
795
|
-
|
|
796
|
-
type: 'RUN_FINISHED',
|
|
797
|
-
...(responseId && { responseId }),
|
|
798
|
-
runId: aguiState.runId,
|
|
799
|
-
threadId: aguiState.threadId,
|
|
800
|
-
model: chunkModel,
|
|
801
|
-
timestamp,
|
|
802
|
-
usage: usage
|
|
803
|
-
? {
|
|
804
|
-
promptTokens: usage.prompt_tokens || 0,
|
|
805
|
-
completionTokens: usage.completion_tokens || 0,
|
|
806
|
-
totalTokens: usage.total_tokens || 0,
|
|
807
|
-
...mistralCachedTokens(usage),
|
|
808
|
-
}
|
|
809
|
-
: undefined,
|
|
810
|
-
finishReason: computedFinishReason,
|
|
811
|
-
})
|
|
850
|
+
finished = { model: chunkModel, finishReason: computedFinishReason }
|
|
812
851
|
}
|
|
813
852
|
}
|
|
814
853
|
|
|
854
|
+
if (finished) {
|
|
855
|
+
yield asChunk({
|
|
856
|
+
type: 'RUN_FINISHED',
|
|
857
|
+
...(responseId && { responseId }),
|
|
858
|
+
runId: aguiState.runId,
|
|
859
|
+
threadId: aguiState.threadId,
|
|
860
|
+
model: finished.model,
|
|
861
|
+
timestamp,
|
|
862
|
+
...(aguiState.usage && { usage: aguiState.usage }),
|
|
863
|
+
finishReason: finished.finishReason,
|
|
864
|
+
})
|
|
865
|
+
}
|
|
866
|
+
|
|
815
867
|
// Stream ended cleanly without finish_reason — flush any open
|
|
816
868
|
// lifecycle events so consumers don't see orphaned starts. This
|
|
817
869
|
// happens for abrupt `[DONE]` or upstream cuts.
|
|
@@ -866,7 +918,7 @@ export class MistralTextAdapter<
|
|
|
866
918
|
threadId: aguiState.threadId,
|
|
867
919
|
model: lastChunkModel,
|
|
868
920
|
timestamp,
|
|
869
|
-
usage:
|
|
921
|
+
...(aguiState.usage && { usage: aguiState.usage }),
|
|
870
922
|
finishReason: hasEmittedToolCall ? 'tool_calls' : 'stop',
|
|
871
923
|
})
|
|
872
924
|
}
|
|
@@ -939,6 +991,7 @@ export class MistralTextAdapter<
|
|
|
939
991
|
body: Record<string, unknown>,
|
|
940
992
|
config: MistralClientConfig,
|
|
941
993
|
wrapFetch: FetchWrapper | undefined,
|
|
994
|
+
signal: AbortSignal | undefined,
|
|
942
995
|
): AsyncGenerator<MistralRawChunk> {
|
|
943
996
|
const serverURL = (
|
|
944
997
|
config.baseURL ??
|
|
@@ -966,6 +1019,7 @@ export class MistralTextAdapter<
|
|
|
966
1019
|
method: 'POST',
|
|
967
1020
|
headers,
|
|
968
1021
|
body: JSON.stringify(body),
|
|
1022
|
+
signal,
|
|
969
1023
|
})
|
|
970
1024
|
|
|
971
1025
|
if (!response.ok) {
|