@core-ai/mistral 0.5.1 → 0.6.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/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { Mistral } from '@mistralai/mistralai';
2
2
  import { ChatModel, EmbeddingModel } from '@core-ai/core-ai';
3
+ import { z } from 'zod';
3
4
 
4
- type MistralProviderOptions = {
5
+ type MistralProviderOptions$1 = {
5
6
  apiKey?: string;
6
7
  baseURL?: string;
7
8
  client?: Mistral;
@@ -10,6 +11,81 @@ type MistralProvider = {
10
11
  chatModel(modelId: string): ChatModel;
11
12
  embeddingModel(modelId: string): EmbeddingModel;
12
13
  };
13
- declare function createMistral(options?: MistralProviderOptions): MistralProvider;
14
+ declare function createMistral(options?: MistralProviderOptions$1): MistralProvider;
14
15
 
15
- export { type MistralProvider, type MistralProviderOptions, createMistral };
16
+ declare const mistralGenerateProviderOptionsSchema: z.ZodObject<{
17
+ stopSequences: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
18
+ frequencyPenalty: z.ZodOptional<z.ZodNumber>;
19
+ presencePenalty: z.ZodOptional<z.ZodNumber>;
20
+ randomSeed: z.ZodOptional<z.ZodNumber>;
21
+ parallelToolCalls: z.ZodOptional<z.ZodBoolean>;
22
+ promptMode: z.ZodOptional<z.ZodString>;
23
+ safePrompt: z.ZodOptional<z.ZodBoolean>;
24
+ }, "strict", z.ZodTypeAny, {
25
+ stopSequences?: string[] | undefined;
26
+ frequencyPenalty?: number | undefined;
27
+ presencePenalty?: number | undefined;
28
+ randomSeed?: number | undefined;
29
+ parallelToolCalls?: boolean | undefined;
30
+ promptMode?: string | undefined;
31
+ safePrompt?: boolean | undefined;
32
+ }, {
33
+ stopSequences?: string[] | undefined;
34
+ frequencyPenalty?: number | undefined;
35
+ presencePenalty?: number | undefined;
36
+ randomSeed?: number | undefined;
37
+ parallelToolCalls?: boolean | undefined;
38
+ promptMode?: string | undefined;
39
+ safePrompt?: boolean | undefined;
40
+ }>;
41
+ type MistralGenerateProviderOptions = z.infer<typeof mistralGenerateProviderOptionsSchema>;
42
+ declare const mistralEmbedProviderOptionsSchema: z.ZodObject<{
43
+ outputDtype: z.ZodOptional<z.ZodEnum<["float", "int8", "uint8", "binary", "ubinary"]>>;
44
+ encodingFormat: z.ZodOptional<z.ZodEnum<["float", "base64"]>>;
45
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
46
+ }, "strict", z.ZodTypeAny, {
47
+ outputDtype?: "float" | "int8" | "uint8" | "binary" | "ubinary" | undefined;
48
+ encodingFormat?: "float" | "base64" | undefined;
49
+ metadata?: Record<string, unknown> | undefined;
50
+ }, {
51
+ outputDtype?: "float" | "int8" | "uint8" | "binary" | "ubinary" | undefined;
52
+ encodingFormat?: "float" | "base64" | undefined;
53
+ metadata?: Record<string, unknown> | undefined;
54
+ }>;
55
+ type MistralEmbedProviderOptions = z.infer<typeof mistralEmbedProviderOptionsSchema>;
56
+ declare module '@core-ai/core-ai' {
57
+ interface GenerateProviderOptions {
58
+ mistral?: MistralGenerateProviderOptions;
59
+ }
60
+ interface EmbedProviderOptions {
61
+ mistral?: MistralEmbedProviderOptions;
62
+ }
63
+ }
64
+ declare const mistralProviderOptionsSchema: z.ZodObject<{
65
+ stopSequences: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
66
+ frequencyPenalty: z.ZodOptional<z.ZodNumber>;
67
+ presencePenalty: z.ZodOptional<z.ZodNumber>;
68
+ randomSeed: z.ZodOptional<z.ZodNumber>;
69
+ parallelToolCalls: z.ZodOptional<z.ZodBoolean>;
70
+ promptMode: z.ZodOptional<z.ZodString>;
71
+ safePrompt: z.ZodOptional<z.ZodBoolean>;
72
+ }, "strict", z.ZodTypeAny, {
73
+ stopSequences?: string[] | undefined;
74
+ frequencyPenalty?: number | undefined;
75
+ presencePenalty?: number | undefined;
76
+ randomSeed?: number | undefined;
77
+ parallelToolCalls?: boolean | undefined;
78
+ promptMode?: string | undefined;
79
+ safePrompt?: boolean | undefined;
80
+ }, {
81
+ stopSequences?: string[] | undefined;
82
+ frequencyPenalty?: number | undefined;
83
+ presencePenalty?: number | undefined;
84
+ randomSeed?: number | undefined;
85
+ parallelToolCalls?: boolean | undefined;
86
+ promptMode?: string | undefined;
87
+ safePrompt?: boolean | undefined;
88
+ }>;
89
+ type MistralProviderOptions = MistralGenerateProviderOptions;
90
+
91
+ export { type MistralEmbedProviderOptions, type MistralGenerateProviderOptions, type MistralProviderOptions as MistralModelProviderOptions, type MistralProvider, type MistralProviderOptions$1 as MistralProviderOptions, createMistral, mistralEmbedProviderOptionsSchema, mistralGenerateProviderOptionsSchema, mistralProviderOptionsSchema };
package/dist/index.js CHANGED
@@ -6,12 +6,46 @@ import {
6
6
  StructuredOutputNoObjectGeneratedError,
7
7
  StructuredOutputParseError,
8
8
  StructuredOutputValidationError,
9
- createObjectStreamResult,
10
- createStreamResult
9
+ createObjectStream,
10
+ createChatStream
11
11
  } from "@core-ai/core-ai";
12
12
 
13
13
  // src/chat-adapter.ts
14
14
  import { zodToJsonSchema } from "zod-to-json-schema";
15
+
16
+ // src/provider-options.ts
17
+ import { z } from "zod";
18
+ var mistralGenerateProviderOptionsSchema = z.object({
19
+ stopSequences: z.array(z.string()).optional(),
20
+ frequencyPenalty: z.number().optional(),
21
+ presencePenalty: z.number().optional(),
22
+ randomSeed: z.number().int().optional(),
23
+ parallelToolCalls: z.boolean().optional(),
24
+ promptMode: z.string().optional(),
25
+ safePrompt: z.boolean().optional()
26
+ }).strict();
27
+ var mistralEmbedProviderOptionsSchema = z.object({
28
+ outputDtype: z.enum(["float", "int8", "uint8", "binary", "ubinary"]).optional(),
29
+ encodingFormat: z.enum(["float", "base64"]).optional(),
30
+ metadata: z.record(z.string(), z.unknown()).optional()
31
+ }).strict();
32
+ function parseMistralGenerateProviderOptions(providerOptions) {
33
+ const rawOptions = providerOptions?.mistral;
34
+ if (rawOptions === void 0) {
35
+ return void 0;
36
+ }
37
+ return mistralGenerateProviderOptionsSchema.parse(rawOptions);
38
+ }
39
+ function parseMistralEmbedProviderOptions(providerOptions) {
40
+ const rawOptions = providerOptions?.mistral;
41
+ if (rawOptions === void 0) {
42
+ return void 0;
43
+ }
44
+ return mistralEmbedProviderOptionsSchema.parse(rawOptions);
45
+ }
46
+ var mistralProviderOptionsSchema = mistralGenerateProviderOptionsSchema;
47
+
48
+ // src/chat-adapter.ts
15
49
  var DEFAULT_STRUCTURED_OUTPUT_TOOL_NAME = "core_ai_generate_object";
16
50
  var DEFAULT_STRUCTURED_OUTPUT_TOOL_DESCRIPTION = "Return a JSON object that matches the requested schema.";
17
51
  function convertMessages(messages) {
@@ -31,13 +65,23 @@ function convertMessage(message) {
31
65
  };
32
66
  }
33
67
  if (message.role === "assistant") {
34
- const text = message.parts.flatMap((part) => part.type === "text" ? [part.text] : []).join("");
68
+ const contentChunks = [];
35
69
  const toolCalls = message.parts.flatMap(
36
70
  (part) => part.type === "tool-call" ? [part.toolCall] : []
37
71
  );
72
+ for (const part of message.parts) {
73
+ if (part.type === "text") {
74
+ contentChunks.push({ type: "text", text: part.text });
75
+ } else if (part.type === "reasoning" && part.text.length > 0) {
76
+ contentChunks.push({
77
+ type: "thinking",
78
+ thinking: [{ type: "text", text: part.text }]
79
+ });
80
+ }
81
+ }
38
82
  return {
39
83
  role: "assistant",
40
- content: text.length > 0 ? text : null,
84
+ content: contentChunks.length > 0 ? contentChunks : null,
41
85
  ...toolCalls.length > 0 ? {
42
86
  toolCalls: toolCalls.map((toolCall) => ({
43
87
  id: toolCall.id,
@@ -100,11 +144,7 @@ function convertToolChoice(choice) {
100
144
  };
101
145
  }
102
146
  function getStructuredOutputToolName(options) {
103
- const trimmedName = options.schemaName?.trim();
104
- if (trimmedName && trimmedName.length > 0) {
105
- return trimmedName;
106
- }
107
- return DEFAULT_STRUCTURED_OUTPUT_TOOL_NAME;
147
+ return options.schemaName?.trim() || DEFAULT_STRUCTURED_OUTPUT_TOOL_NAME;
108
148
  }
109
149
  function createStructuredOutputOptions(options) {
110
150
  const toolName = getStructuredOutputToolName(options);
@@ -122,23 +162,31 @@ function createStructuredOutputOptions(options) {
122
162
  toolName
123
163
  },
124
164
  reasoning: options.reasoning,
125
- config: options.config,
165
+ temperature: options.temperature,
166
+ maxTokens: options.maxTokens,
167
+ topP: options.topP,
126
168
  providerOptions: options.providerOptions,
127
169
  signal: options.signal
128
170
  };
129
171
  }
130
172
  function createGenerateRequest(modelId, options) {
173
+ const mistralOptions = parseMistralGenerateProviderOptions(
174
+ options.providerOptions
175
+ );
131
176
  const baseRequest = {
132
177
  ...createRequestBase(modelId, options)
133
178
  };
134
- return mergeProviderOptions(baseRequest, options.providerOptions);
179
+ return mapMistralProviderOptionsToRequest(baseRequest, mistralOptions);
135
180
  }
136
181
  function createStreamRequest(modelId, options) {
182
+ const mistralOptions = parseMistralGenerateProviderOptions(
183
+ options.providerOptions
184
+ );
137
185
  const baseRequest = {
138
186
  ...createRequestBase(modelId, options),
139
187
  stream: true
140
188
  };
141
- return mergeProviderOptions(baseRequest, options.providerOptions);
189
+ return mapMistralProviderOptionsToRequest(baseRequest, mistralOptions);
142
190
  }
143
191
  function createRequestBase(modelId, options) {
144
192
  return {
@@ -146,24 +194,30 @@ function createRequestBase(modelId, options) {
146
194
  messages: convertMessages(options.messages),
147
195
  ...options.tools && Object.keys(options.tools).length > 0 ? { tools: convertTools(options.tools) } : {},
148
196
  ...options.toolChoice ? { toolChoice: convertToolChoice(options.toolChoice) } : {},
149
- ...mapConfigToRequestFields(options.config)
197
+ ...mapSamplingToRequestFields(options)
150
198
  };
151
199
  }
152
- function mapConfigToRequestFields(config) {
200
+ function mapSamplingToRequestFields(options) {
153
201
  return {
154
- ...config?.temperature !== void 0 ? { temperature: config.temperature } : {},
155
- ...config?.maxTokens !== void 0 ? { maxTokens: config.maxTokens } : {},
156
- ...config?.topP !== void 0 ? { topP: config.topP } : {},
157
- ...config?.stopSequences ? { stop: config.stopSequences } : {},
158
- ...config?.frequencyPenalty !== void 0 ? { frequencyPenalty: config.frequencyPenalty } : {},
159
- ...config?.presencePenalty !== void 0 ? { presencePenalty: config.presencePenalty } : {}
202
+ ...options.temperature !== void 0 ? { temperature: options.temperature } : {},
203
+ ...options.maxTokens !== void 0 ? { maxTokens: options.maxTokens } : {},
204
+ ...options.topP !== void 0 ? { topP: options.topP } : {}
160
205
  };
161
206
  }
162
- function mergeProviderOptions(baseRequest, providerOptions) {
163
- return providerOptions ? {
207
+ function mapMistralProviderOptionsToRequest(baseRequest, providerOptions) {
208
+ if (!providerOptions) {
209
+ return baseRequest;
210
+ }
211
+ return {
164
212
  ...baseRequest,
165
- ...providerOptions
166
- } : baseRequest;
213
+ ...providerOptions.stopSequences ? { stop: providerOptions.stopSequences } : {},
214
+ ...providerOptions.frequencyPenalty !== void 0 ? { frequencyPenalty: providerOptions.frequencyPenalty } : {},
215
+ ...providerOptions.presencePenalty !== void 0 ? { presencePenalty: providerOptions.presencePenalty } : {},
216
+ ...providerOptions.randomSeed !== void 0 ? { randomSeed: providerOptions.randomSeed } : {},
217
+ ...providerOptions.parallelToolCalls !== void 0 ? { parallelToolCalls: providerOptions.parallelToolCalls } : {},
218
+ ...providerOptions.promptMode !== void 0 ? { promptMode: providerOptions.promptMode } : {},
219
+ ...providerOptions.safePrompt !== void 0 ? { safePrompt: providerOptions.safePrompt } : {}
220
+ };
167
221
  }
168
222
  function mapGenerateResponse(response) {
169
223
  const firstChoice = response.choices[0];
@@ -207,6 +261,16 @@ async function* transformStream(stream) {
207
261
  },
208
262
  outputTokenDetails: {}
209
263
  };
264
+ const closeReasoning = () => {
265
+ if (!reasoningOpen) {
266
+ return null;
267
+ }
268
+ reasoningOpen = false;
269
+ return {
270
+ type: "reasoning-end",
271
+ providerMetadata: { mistral: {} }
272
+ };
273
+ };
210
274
  for await (const event of stream) {
211
275
  const chunk = event.data;
212
276
  if (chunk.usage) {
@@ -232,11 +296,9 @@ async function* transformStream(stream) {
232
296
  }
233
297
  }
234
298
  for (const textDelta of extractTextDeltas(choice.delta.content)) {
235
- if (reasoningOpen) {
236
- reasoningOpen = false;
237
- yield {
238
- type: "reasoning-end"
239
- };
299
+ const reasoningEnd2 = closeReasoning();
300
+ if (reasoningEnd2) {
301
+ yield reasoningEnd2;
240
302
  }
241
303
  yield {
242
304
  type: "text-delta",
@@ -244,11 +306,9 @@ async function* transformStream(stream) {
244
306
  };
245
307
  }
246
308
  if (choice.delta.toolCalls) {
247
- if (reasoningOpen) {
248
- reasoningOpen = false;
249
- yield {
250
- type: "reasoning-end"
251
- };
309
+ const reasoningEnd2 = closeReasoning();
310
+ if (reasoningEnd2) {
311
+ yield reasoningEnd2;
252
312
  }
253
313
  for (const [
254
314
  position,
@@ -303,10 +363,9 @@ async function* transformStream(stream) {
303
363
  yield* emitBufferedToolCalls(bufferedToolCalls, emittedToolCalls);
304
364
  }
305
365
  }
306
- if (reasoningOpen) {
307
- yield {
308
- type: "reasoning-end"
309
- };
366
+ const reasoningEnd = closeReasoning();
367
+ if (reasoningEnd) {
368
+ yield reasoningEnd;
310
369
  }
311
370
  yield* emitBufferedToolCalls(bufferedToolCalls, emittedToolCalls);
312
371
  yield {
@@ -386,7 +445,8 @@ function extractAssistantParts(message) {
386
445
  const thinkingText = extractThinkingText(chunk.thinking);
387
446
  parts.push({
388
447
  type: "reasoning",
389
- text: thinkingText
448
+ text: thinkingText,
449
+ providerMetadata: { mistral: {} }
390
450
  });
391
451
  }
392
452
  }
@@ -498,16 +558,22 @@ function createMistralChatModel(client, modelId) {
498
558
  async function generateChat(options) {
499
559
  const request = createGenerateRequest(modelId, options);
500
560
  const response = await callMistralChatApi(
501
- () => client.chat.complete(request)
561
+ () => client.chat.complete(request, { signal: options.signal })
502
562
  );
503
563
  return mapGenerateResponse(response);
504
564
  }
505
565
  async function streamChat(options) {
506
566
  const request = createStreamRequest(modelId, options);
507
- const stream = await callMistralChatApi(
508
- () => client.chat.stream(request)
567
+ return createChatStream(
568
+ async () => transformStream(
569
+ await callMistralChatApi(
570
+ () => client.chat.stream(request, {
571
+ signal: options.signal
572
+ })
573
+ )
574
+ ),
575
+ { signal: options.signal }
509
576
  );
510
- return createStreamResult(transformStream(stream));
511
577
  }
512
578
  return {
513
579
  provider,
@@ -534,13 +600,16 @@ function createMistralChatModel(client, modelId) {
534
600
  const structuredOptions = createStructuredOutputOptions(options);
535
601
  const stream = await streamChat(structuredOptions);
536
602
  const toolName = getStructuredOutputToolName(options);
537
- return createObjectStreamResult(
603
+ return createObjectStream(
538
604
  transformStructuredOutputStream(
539
605
  stream,
540
606
  options.schema,
541
607
  provider,
542
608
  toolName
543
- )
609
+ ),
610
+ {
611
+ signal: options.signal
612
+ }
544
613
  );
545
614
  }
546
615
  };
@@ -655,7 +724,12 @@ function validateStructuredToolArguments(schema, toolArguments, provider) {
655
724
  }
656
725
  function parseAndValidateStructuredPayload(schema, rawPayload, provider) {
657
726
  const parsedPayload = parseJson(rawPayload, provider);
658
- return validateStructuredObject(schema, parsedPayload, provider, rawPayload);
727
+ return validateStructuredObject(
728
+ schema,
729
+ parsedPayload,
730
+ provider,
731
+ rawPayload
732
+ );
659
733
  }
660
734
  function parseJson(rawOutput, provider) {
661
735
  try {
@@ -704,9 +778,12 @@ function createMistralEmbeddingModel(client, modelId) {
704
778
  inputs: options.input,
705
779
  ...options.dimensions !== void 0 ? { outputDimension: options.dimensions } : {}
706
780
  };
707
- const request = options.providerOptions ? {
781
+ const mistralOptions = parseMistralEmbedProviderOptions(
782
+ options.providerOptions
783
+ );
784
+ const request = mistralOptions ? {
708
785
  ...baseRequest,
709
- ...options.providerOptions
786
+ ...mistralOptions
710
787
  } : baseRequest;
711
788
  const response = await client.embeddings.create(request);
712
789
  return {
@@ -734,5 +811,8 @@ function createMistral(options = {}) {
734
811
  };
735
812
  }
736
813
  export {
737
- createMistral
814
+ createMistral,
815
+ mistralEmbedProviderOptionsSchema,
816
+ mistralGenerateProviderOptionsSchema,
817
+ mistralProviderOptionsSchema
738
818
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@core-ai/mistral",
3
- "version": "0.5.1",
3
+ "version": "0.6.1",
4
4
  "description": "Mistral provider package for @core-ai/core-ai",
5
5
  "license": "MIT",
6
6
  "author": "Omnifact (https://omnifact.ai)",
@@ -41,7 +41,7 @@
41
41
  "test:watch": "vitest"
42
42
  },
43
43
  "dependencies": {
44
- "@core-ai/core-ai": "^0.5.1",
44
+ "@core-ai/core-ai": "^0.6.1",
45
45
  "@mistralai/mistralai": "^1.14.0",
46
46
  "zod-to-json-schema": "^3.25.1"
47
47
  },
@@ -49,8 +49,9 @@
49
49
  "zod": "^3.25.0 || ^4.0.0"
50
50
  },
51
51
  "devDependencies": {
52
- "@core-ai/eslint-config": "^0.0.0",
53
- "@core-ai/typescript-config": "^0.0.0",
52
+ "@core-ai/eslint-config": "*",
53
+ "@core-ai/testing": "*",
54
+ "@core-ai/typescript-config": "*",
54
55
  "typescript": "^5.7.3",
55
56
  "vitest": "^3.2.4"
56
57
  }