@core-ai/mistral 0.5.1 → 0.6.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/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,
@@ -122,23 +166,31 @@ function createStructuredOutputOptions(options) {
122
166
  toolName
123
167
  },
124
168
  reasoning: options.reasoning,
125
- config: options.config,
169
+ temperature: options.temperature,
170
+ maxTokens: options.maxTokens,
171
+ topP: options.topP,
126
172
  providerOptions: options.providerOptions,
127
173
  signal: options.signal
128
174
  };
129
175
  }
130
176
  function createGenerateRequest(modelId, options) {
177
+ const mistralOptions = parseMistralGenerateProviderOptions(
178
+ options.providerOptions
179
+ );
131
180
  const baseRequest = {
132
181
  ...createRequestBase(modelId, options)
133
182
  };
134
- return mergeProviderOptions(baseRequest, options.providerOptions);
183
+ return mapMistralProviderOptionsToRequest(baseRequest, mistralOptions);
135
184
  }
136
185
  function createStreamRequest(modelId, options) {
186
+ const mistralOptions = parseMistralGenerateProviderOptions(
187
+ options.providerOptions
188
+ );
137
189
  const baseRequest = {
138
190
  ...createRequestBase(modelId, options),
139
191
  stream: true
140
192
  };
141
- return mergeProviderOptions(baseRequest, options.providerOptions);
193
+ return mapMistralProviderOptionsToRequest(baseRequest, mistralOptions);
142
194
  }
143
195
  function createRequestBase(modelId, options) {
144
196
  return {
@@ -146,24 +198,31 @@ function createRequestBase(modelId, options) {
146
198
  messages: convertMessages(options.messages),
147
199
  ...options.tools && Object.keys(options.tools).length > 0 ? { tools: convertTools(options.tools) } : {},
148
200
  ...options.toolChoice ? { toolChoice: convertToolChoice(options.toolChoice) } : {},
149
- ...mapConfigToRequestFields(options.config)
201
+ ...mapSamplingToRequestFields(options)
150
202
  };
151
203
  }
152
- function mapConfigToRequestFields(config) {
204
+ function mapSamplingToRequestFields(options) {
153
205
  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 } : {}
206
+ ...options.temperature !== void 0 ? { temperature: options.temperature } : {},
207
+ ...options.maxTokens !== void 0 ? { maxTokens: options.maxTokens } : {},
208
+ ...options.topP !== void 0 ? { topP: options.topP } : {}
160
209
  };
161
210
  }
162
- function mergeProviderOptions(baseRequest, providerOptions) {
163
- return providerOptions ? {
211
+ function mapMistralProviderOptionsToRequest(baseRequest, providerOptions) {
212
+ if (!providerOptions) {
213
+ return baseRequest;
214
+ }
215
+ const mergedRequest = {
164
216
  ...baseRequest,
165
- ...providerOptions
166
- } : baseRequest;
217
+ ...providerOptions.stopSequences ? { stop: providerOptions.stopSequences } : {},
218
+ ...providerOptions.frequencyPenalty !== void 0 ? { frequencyPenalty: providerOptions.frequencyPenalty } : {},
219
+ ...providerOptions.presencePenalty !== void 0 ? { presencePenalty: providerOptions.presencePenalty } : {},
220
+ ...providerOptions.randomSeed !== void 0 ? { randomSeed: providerOptions.randomSeed } : {},
221
+ ...providerOptions.parallelToolCalls !== void 0 ? { parallelToolCalls: providerOptions.parallelToolCalls } : {},
222
+ ...providerOptions.promptMode !== void 0 ? { promptMode: providerOptions.promptMode } : {},
223
+ ...providerOptions.safePrompt !== void 0 ? { safePrompt: providerOptions.safePrompt } : {}
224
+ };
225
+ return mergedRequest;
167
226
  }
168
227
  function mapGenerateResponse(response) {
169
228
  const firstChoice = response.choices[0];
@@ -235,7 +294,8 @@ async function* transformStream(stream) {
235
294
  if (reasoningOpen) {
236
295
  reasoningOpen = false;
237
296
  yield {
238
- type: "reasoning-end"
297
+ type: "reasoning-end",
298
+ providerMetadata: { mistral: {} }
239
299
  };
240
300
  }
241
301
  yield {
@@ -247,7 +307,8 @@ async function* transformStream(stream) {
247
307
  if (reasoningOpen) {
248
308
  reasoningOpen = false;
249
309
  yield {
250
- type: "reasoning-end"
310
+ type: "reasoning-end",
311
+ providerMetadata: { mistral: {} }
251
312
  };
252
313
  }
253
314
  for (const [
@@ -305,7 +366,8 @@ async function* transformStream(stream) {
305
366
  }
306
367
  if (reasoningOpen) {
307
368
  yield {
308
- type: "reasoning-end"
369
+ type: "reasoning-end",
370
+ providerMetadata: { mistral: {} }
309
371
  };
310
372
  }
311
373
  yield* emitBufferedToolCalls(bufferedToolCalls, emittedToolCalls);
@@ -386,7 +448,8 @@ function extractAssistantParts(message) {
386
448
  const thinkingText = extractThinkingText(chunk.thinking);
387
449
  parts.push({
388
450
  type: "reasoning",
389
- text: thinkingText
451
+ text: thinkingText,
452
+ providerMetadata: { mistral: {} }
390
453
  });
391
454
  }
392
455
  }
@@ -498,16 +561,22 @@ function createMistralChatModel(client, modelId) {
498
561
  async function generateChat(options) {
499
562
  const request = createGenerateRequest(modelId, options);
500
563
  const response = await callMistralChatApi(
501
- () => client.chat.complete(request)
564
+ () => client.chat.complete(request, { signal: options.signal })
502
565
  );
503
566
  return mapGenerateResponse(response);
504
567
  }
505
568
  async function streamChat(options) {
506
569
  const request = createStreamRequest(modelId, options);
507
- const stream = await callMistralChatApi(
508
- () => client.chat.stream(request)
570
+ return createChatStream(
571
+ async () => transformStream(
572
+ await callMistralChatApi(
573
+ () => client.chat.stream(request, {
574
+ signal: options.signal
575
+ })
576
+ )
577
+ ),
578
+ { signal: options.signal }
509
579
  );
510
- return createStreamResult(transformStream(stream));
511
580
  }
512
581
  return {
513
582
  provider,
@@ -534,13 +603,16 @@ function createMistralChatModel(client, modelId) {
534
603
  const structuredOptions = createStructuredOutputOptions(options);
535
604
  const stream = await streamChat(structuredOptions);
536
605
  const toolName = getStructuredOutputToolName(options);
537
- return createObjectStreamResult(
606
+ return createObjectStream(
538
607
  transformStructuredOutputStream(
539
608
  stream,
540
609
  options.schema,
541
610
  provider,
542
611
  toolName
543
- )
612
+ ),
613
+ {
614
+ signal: options.signal
615
+ }
544
616
  );
545
617
  }
546
618
  };
@@ -655,7 +727,12 @@ function validateStructuredToolArguments(schema, toolArguments, provider) {
655
727
  }
656
728
  function parseAndValidateStructuredPayload(schema, rawPayload, provider) {
657
729
  const parsedPayload = parseJson(rawPayload, provider);
658
- return validateStructuredObject(schema, parsedPayload, provider, rawPayload);
730
+ return validateStructuredObject(
731
+ schema,
732
+ parsedPayload,
733
+ provider,
734
+ rawPayload
735
+ );
659
736
  }
660
737
  function parseJson(rawOutput, provider) {
661
738
  try {
@@ -704,9 +781,12 @@ function createMistralEmbeddingModel(client, modelId) {
704
781
  inputs: options.input,
705
782
  ...options.dimensions !== void 0 ? { outputDimension: options.dimensions } : {}
706
783
  };
707
- const request = options.providerOptions ? {
784
+ const mistralOptions = parseMistralEmbedProviderOptions(
785
+ options.providerOptions
786
+ );
787
+ const request = mistralOptions ? {
708
788
  ...baseRequest,
709
- ...options.providerOptions
789
+ ...mistralOptions
710
790
  } : baseRequest;
711
791
  const response = await client.embeddings.create(request);
712
792
  return {
@@ -734,5 +814,8 @@ function createMistral(options = {}) {
734
814
  };
735
815
  }
736
816
  export {
737
- createMistral
817
+ createMistral,
818
+ mistralEmbedProviderOptionsSchema,
819
+ mistralGenerateProviderOptionsSchema,
820
+ mistralProviderOptionsSchema
738
821
  };
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.0",
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.0",
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
  }