@node-llm/core 1.12.0 → 1.14.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.
Files changed (73) hide show
  1. package/README.md +11 -10
  2. package/dist/aliases.d.ts +187 -63
  3. package/dist/aliases.d.ts.map +1 -1
  4. package/dist/aliases.js +245 -121
  5. package/dist/config.d.ts +16 -0
  6. package/dist/config.d.ts.map +1 -1
  7. package/dist/config.js +29 -1
  8. package/dist/constants.d.ts +2 -0
  9. package/dist/constants.d.ts.map +1 -1
  10. package/dist/constants.js +2 -0
  11. package/dist/llm.d.ts.map +1 -1
  12. package/dist/llm.js +4 -2
  13. package/dist/models/models.json +2919 -867
  14. package/dist/models/types.d.ts +1 -1
  15. package/dist/models/types.d.ts.map +1 -1
  16. package/dist/providers/mistral/Capabilities.d.ts +19 -0
  17. package/dist/providers/mistral/Capabilities.d.ts.map +1 -0
  18. package/dist/providers/mistral/Capabilities.js +113 -0
  19. package/dist/providers/mistral/Chat.d.ts +8 -0
  20. package/dist/providers/mistral/Chat.d.ts.map +1 -0
  21. package/dist/providers/mistral/Chat.js +149 -0
  22. package/dist/providers/mistral/Embedding.d.ts +8 -0
  23. package/dist/providers/mistral/Embedding.d.ts.map +1 -0
  24. package/dist/providers/mistral/Embedding.js +44 -0
  25. package/dist/providers/mistral/Errors.d.ts +2 -0
  26. package/dist/providers/mistral/Errors.d.ts.map +1 -0
  27. package/dist/providers/mistral/Errors.js +57 -0
  28. package/dist/providers/mistral/MistralProvider.d.ts +40 -0
  29. package/dist/providers/mistral/MistralProvider.d.ts.map +1 -0
  30. package/dist/providers/mistral/MistralProvider.js +82 -0
  31. package/dist/providers/mistral/Models.d.ts +8 -0
  32. package/dist/providers/mistral/Models.d.ts.map +1 -0
  33. package/dist/providers/mistral/Models.js +71 -0
  34. package/dist/providers/mistral/Moderation.d.ts +8 -0
  35. package/dist/providers/mistral/Moderation.d.ts.map +1 -0
  36. package/dist/providers/mistral/Moderation.js +49 -0
  37. package/dist/providers/mistral/Streaming.d.ts +8 -0
  38. package/dist/providers/mistral/Streaming.d.ts.map +1 -0
  39. package/dist/providers/mistral/Streaming.js +203 -0
  40. package/dist/providers/mistral/Transcription.d.ts +9 -0
  41. package/dist/providers/mistral/Transcription.d.ts.map +1 -0
  42. package/dist/providers/mistral/Transcription.js +76 -0
  43. package/dist/providers/mistral/index.d.ts +10 -0
  44. package/dist/providers/mistral/index.d.ts.map +1 -0
  45. package/dist/providers/mistral/index.js +26 -0
  46. package/dist/providers/registry.d.ts +3 -1
  47. package/dist/providers/registry.d.ts.map +1 -1
  48. package/dist/providers/registry.js +3 -1
  49. package/dist/providers/xai/Capabilities.d.ts +12 -0
  50. package/dist/providers/xai/Capabilities.d.ts.map +1 -0
  51. package/dist/providers/xai/Capabilities.js +79 -0
  52. package/dist/providers/xai/Chat.d.ts +8 -0
  53. package/dist/providers/xai/Chat.d.ts.map +1 -0
  54. package/dist/providers/xai/Chat.js +69 -0
  55. package/dist/providers/xai/Errors.d.ts +2 -0
  56. package/dist/providers/xai/Errors.d.ts.map +1 -0
  57. package/dist/providers/xai/Errors.js +33 -0
  58. package/dist/providers/xai/Image.d.ts +8 -0
  59. package/dist/providers/xai/Image.d.ts.map +1 -0
  60. package/dist/providers/xai/Image.js +47 -0
  61. package/dist/providers/xai/Models.d.ts +8 -0
  62. package/dist/providers/xai/Models.d.ts.map +1 -0
  63. package/dist/providers/xai/Models.js +47 -0
  64. package/dist/providers/xai/Streaming.d.ts +8 -0
  65. package/dist/providers/xai/Streaming.d.ts.map +1 -0
  66. package/dist/providers/xai/Streaming.js +167 -0
  67. package/dist/providers/xai/XAIProvider.d.ts +37 -0
  68. package/dist/providers/xai/XAIProvider.d.ts.map +1 -0
  69. package/dist/providers/xai/XAIProvider.js +66 -0
  70. package/dist/providers/xai/index.d.ts +7 -0
  71. package/dist/providers/xai/index.d.ts.map +1 -0
  72. package/dist/providers/xai/index.js +19 -0
  73. package/package.json +1 -1
@@ -47,7 +47,7 @@ export interface Model {
47
47
  pricing?: ModelPricing;
48
48
  metadata?: Record<string, unknown>;
49
49
  }
50
- export type ProviderName = "openai" | "anthropic" | "gemini" | "deepseek" | "openrouter" | "ollama" | (string & {});
50
+ export type ProviderName = "openai" | "anthropic" | "gemini" | "deepseek" | "openrouter" | "ollama" | "mistral" | (string & {});
51
51
  export type ModelCapability = "streaming" | "function_calling" | "structured_output" | "predicted_outputs" | "distillation" | "fine_tuning" | "batch" | "realtime" | "image_generation" | "speech_generation" | "transcription" | "translation" | "citations" | "reasoning" | "caching" | "moderation" | "json_mode" | "vision" | "chat" | "tools";
52
52
  export type ModelModality = "text" | "image" | "audio" | "pdf" | "video" | "file" | "embeddings" | "moderation";
53
53
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/models/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,YAAY;IAC3B,WAAW,CAAC,EAAE;QACZ,QAAQ,CAAC,EAAE;YACT,iBAAiB,CAAC,EAAE,MAAM,CAAC;YAC3B,wBAAwB,CAAC,EAAE,MAAM,CAAC;YAClC,kBAAkB,CAAC,EAAE,MAAM,CAAC;YAC5B,4BAA4B,CAAC,EAAE,MAAM,CAAC;SACvC,CAAC;QACF,KAAK,CAAC,EAAE;YACN,iBAAiB,CAAC,EAAE,MAAM,CAAC;YAC3B,wBAAwB,CAAC,EAAE,MAAM,CAAC;YAClC,kBAAkB,CAAC,EAAE,MAAM,CAAC;YAC5B,4BAA4B,CAAC,EAAE,MAAM,CAAC;SACvC,CAAC;KACH,CAAC;IACF,MAAM,CAAC,EAAE;QACP,QAAQ,CAAC,EAAE;YACT,KAAK,CAAC,EAAE,MAAM,CAAC;YACf,MAAM,CAAC,EAAE,MAAM,CAAC;SACjB,CAAC;QACF,KAAK,CAAC,EAAE;YACN,KAAK,CAAC,EAAE,MAAM,CAAC;YACf,MAAM,CAAC,EAAE,MAAM,CAAC;SACjB,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C;IAC/B,iBAAiB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,gBAAgB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC,UAAU,EAAE;QACV,KAAK,EAAE,MAAM,EAAE,CAAC;QAChB,MAAM,EAAE,MAAM,EAAE,CAAC;KAClB,CAAC;IACF,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,OAAO,CAAC,EAAE,YAAY,CAAC;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED,MAAM,MAAM,YAAY,GACpB,QAAQ,GACR,WAAW,GACX,QAAQ,GACR,UAAU,GACV,YAAY,GACZ,QAAQ,GACR,SAAS,GACT,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;AAElB,MAAM,MAAM,eAAe,GACvB,WAAW,GACX,kBAAkB,GAClB,mBAAmB,GACnB,mBAAmB,GACnB,cAAc,GACd,aAAa,GACb,OAAO,GACP,UAAU,GACV,kBAAkB,GAClB,mBAAmB,GACnB,eAAe,GACf,aAAa,GACb,WAAW,GACX,WAAW,GACX,SAAS,GACT,YAAY,GACZ,WAAW,GACX,QAAQ,GACR,MAAM,GACN,OAAO,CAAC;AAEZ,MAAM,MAAM,aAAa,GACrB,MAAM,GACN,OAAO,GACP,OAAO,GACP,KAAK,GACL,OAAO,GACP,MAAM,GACN,YAAY,GACZ,YAAY,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { ModelPricing } from "../../models/types.js";
2
+ export declare class MistralCapabilities {
3
+ static getCapabilities(modelId: string): string[];
4
+ static getContextWindow(modelId: string): number | null;
5
+ static getMaxOutputTokens(modelId: string): number | null;
6
+ static supportsVision(modelId: string): boolean;
7
+ static supportsTools(modelId: string): boolean;
8
+ static supportsStructuredOutput(modelId: string): boolean;
9
+ static supportsEmbeddings(modelId: string): boolean;
10
+ static supportsImageGeneration(_modelId: string): boolean;
11
+ static supportsTranscription(modelId: string): boolean;
12
+ static supportsModeration(modelId: string): boolean;
13
+ static supportsReasoning(modelId: string): boolean;
14
+ static findModel(modelId: string): import("../../models/types.js").Model | undefined;
15
+ static getPricing(modelId: string): ModelPricing | undefined;
16
+ static getInputModalities(modelId: string): string[];
17
+ static getOutputModalities(modelId: string): string[];
18
+ }
19
+ //# sourceMappingURL=Capabilities.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Capabilities.d.ts","sourceRoot":"","sources":["../../../src/providers/mistral/Capabilities.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAErD,qBAAa,mBAAmB;IAC9B,MAAM,CAAC,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE;IAkBjD,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAcvD,MAAM,CAAC,kBAAkB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAQzD,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAQ/C,MAAM,CAAC,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAS9C,MAAM,CAAC,wBAAwB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAQzD,MAAM,CAAC,kBAAkB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAOnD,MAAM,CAAC,uBAAuB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;IAIzD,MAAM,CAAC,qBAAqB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAQtD,MAAM,CAAC,kBAAkB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAOnD,MAAM,CAAC,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAQlD,MAAM,CAAC,SAAS,CAAC,OAAO,EAAE,MAAM;IAIhC,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;IAO5D,MAAM,CAAC,kBAAkB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE;IASpD,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE;CAOtD"}
@@ -0,0 +1,113 @@
1
+ import { ModelRegistry } from "../../models/ModelRegistry.js";
2
+ import { PricingRegistry } from "../../models/PricingRegistry.js";
3
+ export class MistralCapabilities {
4
+ static getCapabilities(modelId) {
5
+ const caps = ["streaming", "chat"];
6
+ if (this.supportsTools(modelId)) {
7
+ caps.push("function_calling", "tools");
8
+ }
9
+ if (this.supportsVision(modelId)) {
10
+ caps.push("vision");
11
+ }
12
+ if (this.supportsStructuredOutput(modelId)) {
13
+ caps.push("structured_output", "json_mode");
14
+ }
15
+ return caps;
16
+ }
17
+ static getContextWindow(modelId) {
18
+ const val = ModelRegistry.getContextWindow(modelId, "mistral");
19
+ if (val)
20
+ return val;
21
+ // Mistral models typically have 32K-128K context
22
+ if (/mistral-large|codestral/.test(modelId)) {
23
+ return 128_000;
24
+ }
25
+ if (/mistral-medium|mistral-small|pixtral/.test(modelId)) {
26
+ return 32_000;
27
+ }
28
+ return 32_000;
29
+ }
30
+ static getMaxOutputTokens(modelId) {
31
+ const model = this.findModel(modelId);
32
+ if (model?.max_output_tokens)
33
+ return model.max_output_tokens;
34
+ // Default max output for Mistral models
35
+ return 8_192;
36
+ }
37
+ static supportsVision(modelId) {
38
+ const model = this.findModel(modelId);
39
+ if (model?.modalities?.input?.includes("image"))
40
+ return true;
41
+ // Pixtral models support vision
42
+ return /pixtral/.test(modelId.toLowerCase());
43
+ }
44
+ static supportsTools(modelId) {
45
+ const model = this.findModel(modelId);
46
+ if (model?.capabilities?.includes("function_calling") || model?.capabilities?.includes("tools"))
47
+ return true;
48
+ // Most Mistral models support tools except embedding models
49
+ return !/embed/.test(modelId.toLowerCase());
50
+ }
51
+ static supportsStructuredOutput(modelId) {
52
+ const model = this.findModel(modelId);
53
+ if (model?.capabilities?.includes("structured_output"))
54
+ return true;
55
+ // Most Mistral chat models support JSON mode
56
+ return !/embed/.test(modelId.toLowerCase());
57
+ }
58
+ static supportsEmbeddings(modelId) {
59
+ const model = this.findModel(modelId);
60
+ if (model?.modalities?.output?.includes("embeddings"))
61
+ return true;
62
+ return /embed/.test(modelId.toLowerCase());
63
+ }
64
+ static supportsImageGeneration(_modelId) {
65
+ return false;
66
+ }
67
+ static supportsTranscription(modelId) {
68
+ const model = this.findModel(modelId);
69
+ if (model?.modalities?.input?.includes("audio"))
70
+ return true;
71
+ // Voxtral models support transcription
72
+ return /voxtral/.test(modelId.toLowerCase());
73
+ }
74
+ static supportsModeration(modelId) {
75
+ const model = this.findModel(modelId);
76
+ if (model?.capabilities?.includes("moderation"))
77
+ return true;
78
+ return /moderation/.test(modelId.toLowerCase());
79
+ }
80
+ static supportsReasoning(modelId) {
81
+ const model = this.findModel(modelId);
82
+ if (model?.capabilities?.includes("reasoning"))
83
+ return true;
84
+ // Magistral models support reasoning
85
+ return /magistral/.test(modelId.toLowerCase());
86
+ }
87
+ static findModel(modelId) {
88
+ return ModelRegistry.find(modelId, "mistral");
89
+ }
90
+ static getPricing(modelId) {
91
+ const model = this.findModel(modelId);
92
+ if (model?.pricing)
93
+ return model.pricing;
94
+ return PricingRegistry.getPricing(modelId, "mistral");
95
+ }
96
+ static getInputModalities(modelId) {
97
+ const model = this.findModel(modelId);
98
+ if (model?.modalities?.input)
99
+ return model.modalities.input;
100
+ const input = ["text"];
101
+ if (this.supportsVision(modelId))
102
+ input.push("image");
103
+ return input;
104
+ }
105
+ static getOutputModalities(modelId) {
106
+ const model = this.findModel(modelId);
107
+ if (model?.modalities?.output)
108
+ return model.modalities.output;
109
+ if (this.supportsEmbeddings(modelId))
110
+ return ["embeddings"];
111
+ return ["text"];
112
+ }
113
+ }
@@ -0,0 +1,8 @@
1
+ import { ChatRequest, ChatResponse } from "../Provider.js";
2
+ export declare class MistralChat {
3
+ private readonly baseUrl;
4
+ private readonly apiKey;
5
+ constructor(baseUrl: string, apiKey: string);
6
+ execute(request: ChatRequest): Promise<ChatResponse>;
7
+ }
8
+ //# sourceMappingURL=Chat.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Chat.d.ts","sourceRoot":"","sources":["../../../src/providers/mistral/Chat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAS,MAAM,gBAAgB,CAAC;AAgDlE,qBAAa,WAAW;IAEpB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM;gBADN,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM;IAG3B,OAAO,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;CAmK3D"}
@@ -0,0 +1,149 @@
1
+ import { ModelRegistry } from "../../models/ModelRegistry.js";
2
+ import { logger } from "../../utils/logger.js";
3
+ import { handleMistralError } from "./Errors.js";
4
+ import { mapSystemMessages } from "../utils.js";
5
+ import { fetchWithTimeout } from "../../utils/fetch.js";
6
+ export class MistralChat {
7
+ baseUrl;
8
+ apiKey;
9
+ constructor(baseUrl, apiKey) {
10
+ this.baseUrl = baseUrl;
11
+ this.apiKey = apiKey;
12
+ }
13
+ async execute(request) {
14
+ const { model, messages, tools, max_tokens, response_format, thinking: _thinking, headers: _headers, requestTimeout, signal: _signal, ...rest } = request;
15
+ const mappedMessages = mapSystemMessages(messages, false);
16
+ // Sanitize messages - remove internal properties that Mistral API doesn't accept
17
+ const sanitizedMessages = mappedMessages.map((msg) => {
18
+ const m = msg;
19
+ const { role, content, tool_calls, tool_call_id, name } = m;
20
+ const clean = { role, content };
21
+ if (tool_calls)
22
+ clean.tool_calls = tool_calls;
23
+ if (tool_call_id)
24
+ clean.tool_call_id = tool_call_id;
25
+ if (name)
26
+ clean.name = name;
27
+ return clean;
28
+ });
29
+ const body = {
30
+ model,
31
+ messages: sanitizedMessages,
32
+ ...rest
33
+ };
34
+ if (max_tokens)
35
+ body.max_tokens = max_tokens;
36
+ if (tools && tools.length > 0)
37
+ body.tools = tools;
38
+ // Handle structured output for Mistral
39
+ if (response_format) {
40
+ if (response_format.type === "json_schema") {
41
+ // Mistral supports JSON mode
42
+ body.response_format = { type: "json_object" };
43
+ // Append schema instructions to the system prompt
44
+ const schema = response_format.json_schema;
45
+ const schemaString = JSON.stringify(schema?.schema, null, 2);
46
+ const instruction = `\n\nIMPORTANT: You must output strictly valid JSON conforming to the following schema:\n${schemaString}\n\nOutput only the JSON object.`;
47
+ const messagesList = body.messages;
48
+ const systemMessage = messagesList.find((m) => m.role === "system");
49
+ if (systemMessage) {
50
+ systemMessage.content += instruction;
51
+ }
52
+ else {
53
+ messagesList.unshift({
54
+ role: "system",
55
+ content: "You are a helpful assistant." + instruction
56
+ });
57
+ }
58
+ }
59
+ else {
60
+ body.response_format = response_format;
61
+ }
62
+ }
63
+ const url = `${this.baseUrl}/chat/completions`;
64
+ logger.logRequest("Mistral", "POST", url, body);
65
+ const response = await fetchWithTimeout(url, {
66
+ method: "POST",
67
+ headers: {
68
+ Authorization: `Bearer ${this.apiKey}`,
69
+ "Content-Type": "application/json",
70
+ Accept: "application/json",
71
+ ...request.headers
72
+ },
73
+ body: JSON.stringify(body)
74
+ }, requestTimeout);
75
+ if (!response.ok) {
76
+ await handleMistralError(response, model);
77
+ }
78
+ const json = (await response.json());
79
+ logger.logResponse("Mistral", response.status, response.statusText, json);
80
+ const choice = json.choices?.[0];
81
+ const message = choice?.message;
82
+ // Parse content - magistral models return array with thinking and text parts
83
+ let textContent = null;
84
+ let reasoningText = null;
85
+ if (Array.isArray(message?.content)) {
86
+ // Magistral models: content is array of parts
87
+ for (const part of message.content) {
88
+ if (part.type === "thinking" && Array.isArray(part.thinking)) {
89
+ // thinking is an array of {type: "text", text: "..."} objects
90
+ for (const thinkingPart of part.thinking) {
91
+ if (thinkingPart.type === "text" && thinkingPart.text) {
92
+ reasoningText = (reasoningText || "") + thinkingPart.text;
93
+ }
94
+ }
95
+ }
96
+ else if (part.type === "text" && part.text) {
97
+ textContent = (textContent || "") + part.text;
98
+ }
99
+ }
100
+ }
101
+ else {
102
+ textContent = message?.content ?? null;
103
+ }
104
+ // Map tool calls to standard format
105
+ const toolCalls = message?.tool_calls?.map((tc) => ({
106
+ id: tc.id,
107
+ type: "function",
108
+ function: {
109
+ name: tc.function.name,
110
+ arguments: tc.function.arguments
111
+ }
112
+ }));
113
+ // Calculate cost if pricing available
114
+ const pricing = ModelRegistry.find(model, "mistral")?.pricing;
115
+ let cost;
116
+ let inputCost;
117
+ let outputCost;
118
+ if (pricing?.text_tokens?.standard && json.usage) {
119
+ const inputPrice = pricing.text_tokens.standard.input_per_million ?? 0;
120
+ const outputPrice = pricing.text_tokens.standard.output_per_million ?? 0;
121
+ inputCost = (json.usage.prompt_tokens / 1_000_000) * inputPrice;
122
+ outputCost = (json.usage.completion_tokens / 1_000_000) * outputPrice;
123
+ cost = inputCost + outputCost;
124
+ }
125
+ const usage = {
126
+ input_tokens: json.usage?.prompt_tokens ?? 0,
127
+ output_tokens: json.usage?.completion_tokens ?? 0,
128
+ total_tokens: json.usage?.total_tokens ?? 0,
129
+ cost,
130
+ input_cost: inputCost,
131
+ output_cost: outputCost
132
+ };
133
+ // Build thinking result for reasoning models
134
+ const thinkingResult = reasoningText
135
+ ? {
136
+ text: reasoningText,
137
+ tokens: undefined
138
+ }
139
+ : undefined;
140
+ return {
141
+ content: textContent,
142
+ tool_calls: toolCalls,
143
+ usage,
144
+ finish_reason: choice?.finish_reason ?? null,
145
+ thinking: thinkingResult,
146
+ reasoning: reasoningText
147
+ };
148
+ }
149
+ }
@@ -0,0 +1,8 @@
1
+ import { EmbeddingRequest, EmbeddingResponse } from "../Provider.js";
2
+ export declare class MistralEmbedding {
3
+ private readonly baseUrl;
4
+ private readonly apiKey;
5
+ constructor(baseUrl: string, apiKey: string);
6
+ execute(request: EmbeddingRequest): Promise<EmbeddingResponse>;
7
+ }
8
+ //# sourceMappingURL=Embedding.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Embedding.d.ts","sourceRoot":"","sources":["../../../src/providers/mistral/Embedding.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAoBrE,qBAAa,gBAAgB;IAEzB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM;gBADN,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM;IAG3B,OAAO,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,iBAAiB,CAAC;CAwCrE"}
@@ -0,0 +1,44 @@
1
+ import { logger } from "../../utils/logger.js";
2
+ import { handleMistralError } from "./Errors.js";
3
+ import { MistralCapabilities } from "./Capabilities.js";
4
+ export class MistralEmbedding {
5
+ baseUrl;
6
+ apiKey;
7
+ constructor(baseUrl, apiKey) {
8
+ this.baseUrl = baseUrl;
9
+ this.apiKey = apiKey;
10
+ }
11
+ async execute(request) {
12
+ const model = request.model || "mistral-embed";
13
+ if (!MistralCapabilities.supportsEmbeddings(model)) {
14
+ throw new Error(`Model ${model} does not support embeddings`);
15
+ }
16
+ const body = {
17
+ model,
18
+ input: request.input,
19
+ encoding_format: "float"
20
+ };
21
+ const url = `${this.baseUrl}/embeddings`;
22
+ logger.logRequest("Mistral Embeddings", "POST", url, body);
23
+ const response = await fetch(url, {
24
+ method: "POST",
25
+ headers: {
26
+ Authorization: `Bearer ${this.apiKey}`,
27
+ "Content-Type": "application/json",
28
+ Accept: "application/json"
29
+ },
30
+ body: JSON.stringify(body)
31
+ });
32
+ if (!response.ok) {
33
+ await handleMistralError(response, model);
34
+ }
35
+ const json = (await response.json());
36
+ logger.logResponse("Mistral Embeddings", response.status, response.statusText, json);
37
+ return {
38
+ model: json.model,
39
+ vectors: json.data.map((item) => item.embedding),
40
+ input_tokens: json.usage?.prompt_tokens ?? 0,
41
+ dimensions: json.data[0]?.embedding?.length ?? 0
42
+ };
43
+ }
44
+ }
@@ -0,0 +1,2 @@
1
+ export declare function handleMistralError(response: Response, model?: string): Promise<never>;
2
+ //# sourceMappingURL=Errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Errors.d.ts","sourceRoot":"","sources":["../../../src/providers/mistral/Errors.ts"],"names":[],"mappings":"AAaA,wBAAsB,kBAAkB,CAAC,QAAQ,EAAE,QAAQ,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,CA6D3F"}
@@ -0,0 +1,57 @@
1
+ import { BadRequestError, ContextWindowExceededError, InsufficientQuotaError, InvalidModelError, NotFoundError, AuthenticationError, RateLimitError, ServerError, ServiceUnavailableError, APIError } from "../../errors/index.js";
2
+ export async function handleMistralError(response, model) {
3
+ const status = response.status;
4
+ let body;
5
+ let message = `Mistral error (${status})`;
6
+ try {
7
+ body = await response.json();
8
+ if (body && typeof body === "object" && "error" in body) {
9
+ const err = body.error;
10
+ if (err && err.message) {
11
+ message = typeof err.message === "string" ? err.message : JSON.stringify(err.message);
12
+ }
13
+ }
14
+ else if (body && typeof body === "object" && "message" in body) {
15
+ message = typeof body.message === "string" ? body.message : JSON.stringify(body.message);
16
+ }
17
+ else if (body && typeof body === "object" && "detail" in body) {
18
+ message = typeof body.detail === "string" ? body.detail : JSON.stringify(body.detail);
19
+ }
20
+ }
21
+ catch {
22
+ body = await response.text().catch(() => "Unknown error");
23
+ message = `Mistral error (${status}): ${body}`;
24
+ }
25
+ const provider = "mistral";
26
+ if (status === 400) {
27
+ if (message.includes("context") || message.includes("length") || message.includes("tokens")) {
28
+ throw new ContextWindowExceededError(message, body, provider, model);
29
+ }
30
+ throw new BadRequestError(message, body, provider, model);
31
+ }
32
+ if (status === 401 || status === 403) {
33
+ throw new AuthenticationError(message, status, body, provider);
34
+ }
35
+ if (status === 404) {
36
+ if (message.includes("model")) {
37
+ throw new InvalidModelError(message, body, provider, model);
38
+ }
39
+ throw new NotFoundError(message, status, body, provider, model);
40
+ }
41
+ if (status === 422) {
42
+ throw new BadRequestError(message, body, provider, model);
43
+ }
44
+ if (status === 429) {
45
+ if (message.includes("quota") || message.includes("billing")) {
46
+ throw new InsufficientQuotaError(message, body, provider);
47
+ }
48
+ throw new RateLimitError(message, status, body, provider);
49
+ }
50
+ if (status >= 500 && status < 600) {
51
+ if (status === 503) {
52
+ throw new ServiceUnavailableError(message, status, body, provider);
53
+ }
54
+ throw new ServerError(message, status, body, provider);
55
+ }
56
+ throw new APIError(message, status, body, provider, model);
57
+ }
@@ -0,0 +1,40 @@
1
+ import { Provider, ChatRequest, ChatResponse, ModelInfo, ChatChunk, EmbeddingRequest, EmbeddingResponse, TranscriptionRequest, TranscriptionResponse, ModerationRequest, ModerationResponse } from "../Provider.js";
2
+ import { BaseProvider } from "../BaseProvider.js";
3
+ export interface MistralProviderOptions {
4
+ apiKey: string;
5
+ baseUrl?: string;
6
+ }
7
+ export declare class MistralProvider extends BaseProvider implements Provider {
8
+ private readonly options;
9
+ private readonly baseUrl;
10
+ private readonly chatHandler;
11
+ private readonly streamingHandler;
12
+ private readonly modelsHandler;
13
+ private readonly embeddingHandler;
14
+ private readonly transcriptionHandler;
15
+ private readonly moderationHandler;
16
+ capabilities: {
17
+ supportsVision: (model: string) => boolean;
18
+ supportsTools: (model: string) => boolean;
19
+ supportsStructuredOutput: (model: string) => boolean;
20
+ supportsEmbeddings: (model: string) => boolean;
21
+ supportsImageGeneration: (model: string) => boolean;
22
+ supportsTranscription: (model: string) => boolean;
23
+ supportsModeration: (model: string) => boolean;
24
+ supportsReasoning: (model: string) => boolean;
25
+ supportsDeveloperRole: (_model: string) => boolean;
26
+ getContextWindow: (model: string) => number | null;
27
+ };
28
+ constructor(options: MistralProviderOptions);
29
+ apiBase(): string;
30
+ headers(): Record<string, string>;
31
+ protected providerName(): string;
32
+ defaultModel(feature?: string): string;
33
+ chat(request: ChatRequest): Promise<ChatResponse>;
34
+ stream(request: ChatRequest): AsyncGenerator<ChatChunk>;
35
+ listModels(): Promise<ModelInfo[]>;
36
+ embed(request: EmbeddingRequest): Promise<EmbeddingResponse>;
37
+ transcribe(request: TranscriptionRequest): Promise<TranscriptionResponse>;
38
+ moderate(request: ModerationRequest): Promise<ModerationResponse>;
39
+ }
40
+ //# sourceMappingURL=MistralProvider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MistralProvider.d.ts","sourceRoot":"","sources":["../../../src/providers/mistral/MistralProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,WAAW,EACX,YAAY,EACZ,SAAS,EACT,SAAS,EACT,gBAAgB,EAChB,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAUlD,MAAM,WAAW,sBAAsB;IACrC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,qBAAa,eAAgB,SAAQ,YAAa,YAAW,QAAQ;IAuBvD,OAAO,CAAC,QAAQ,CAAC,OAAO;IAtBpC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAc;IAC1C,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAmB;IACpD,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAgB;IAC9C,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAmB;IACpD,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAAuB;IAC5D,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAoB;IAE/C,YAAY;gCACO,MAAM;+BACP,MAAM;0CACK,MAAM;oCAEZ,MAAM;yCACD,MAAM;uCACR,MAAM;oCACT,MAAM;mCACP,MAAM;wCACD,MAAM;kCACZ,MAAM;MAChC;gBAE2B,OAAO,EAAE,sBAAsB;IAWrD,OAAO,IAAI,MAAM;IAIjB,OAAO,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAQxC,SAAS,CAAC,YAAY,IAAI,MAAM;IAIhB,YAAY,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM;IAOhD,IAAI,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;IAIhD,MAAM,CAAC,OAAO,EAAE,WAAW,GAAG,cAAc,CAAC,SAAS,CAAC;IAIxD,UAAU,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;IAIlC,KAAK,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAI5D,UAAU,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,qBAAqB,CAAC;IAIzE,QAAQ,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,kBAAkB,CAAC;CAGxE"}
@@ -0,0 +1,82 @@
1
+ import { BaseProvider } from "../BaseProvider.js";
2
+ import { DEFAULT_MISTRAL_BASE_URL } from "../../constants.js";
3
+ import { MistralChat } from "./Chat.js";
4
+ import { MistralModels } from "./Models.js";
5
+ import { MistralStreaming } from "./Streaming.js";
6
+ import { MistralEmbedding } from "./Embedding.js";
7
+ import { MistralCapabilities } from "./Capabilities.js";
8
+ import { MistralTranscription } from "./Transcription.js";
9
+ import { MistralModeration } from "./Moderation.js";
10
+ export class MistralProvider extends BaseProvider {
11
+ options;
12
+ baseUrl;
13
+ chatHandler;
14
+ streamingHandler;
15
+ modelsHandler;
16
+ embeddingHandler;
17
+ transcriptionHandler;
18
+ moderationHandler;
19
+ capabilities = {
20
+ supportsVision: (model) => MistralCapabilities.supportsVision(model),
21
+ supportsTools: (model) => MistralCapabilities.supportsTools(model),
22
+ supportsStructuredOutput: (model) => MistralCapabilities.supportsStructuredOutput(model),
23
+ supportsEmbeddings: (model) => MistralCapabilities.supportsEmbeddings(model),
24
+ supportsImageGeneration: (model) => MistralCapabilities.supportsImageGeneration(model),
25
+ supportsTranscription: (model) => MistralCapabilities.supportsTranscription(model),
26
+ supportsModeration: (model) => MistralCapabilities.supportsModeration(model),
27
+ supportsReasoning: (model) => MistralCapabilities.supportsReasoning(model),
28
+ supportsDeveloperRole: (_model) => false,
29
+ getContextWindow: (model) => MistralCapabilities.getContextWindow(model)
30
+ };
31
+ constructor(options) {
32
+ super();
33
+ this.options = options;
34
+ this.baseUrl = options.baseUrl ?? DEFAULT_MISTRAL_BASE_URL;
35
+ this.chatHandler = new MistralChat(this.baseUrl, options.apiKey);
36
+ this.streamingHandler = new MistralStreaming(this.baseUrl, options.apiKey);
37
+ this.modelsHandler = new MistralModels(this.baseUrl, options.apiKey);
38
+ this.embeddingHandler = new MistralEmbedding(this.baseUrl, options.apiKey);
39
+ this.transcriptionHandler = new MistralTranscription(this.baseUrl, options.apiKey);
40
+ this.moderationHandler = new MistralModeration(this.baseUrl, options.apiKey);
41
+ }
42
+ apiBase() {
43
+ return this.baseUrl;
44
+ }
45
+ headers() {
46
+ return {
47
+ Authorization: `Bearer ${this.options.apiKey}`,
48
+ "Content-Type": "application/json",
49
+ Accept: "application/json"
50
+ };
51
+ }
52
+ providerName() {
53
+ return "Mistral";
54
+ }
55
+ defaultModel(feature) {
56
+ if (feature === "embedding" || feature === "embeddings")
57
+ return "mistral-embed";
58
+ if (feature === "transcription")
59
+ return "voxtral-mini-latest";
60
+ if (feature === "moderation")
61
+ return "mistral-moderation-latest";
62
+ return "mistral-large-latest";
63
+ }
64
+ async chat(request) {
65
+ return this.chatHandler.execute(request);
66
+ }
67
+ async *stream(request) {
68
+ yield* this.streamingHandler.execute(request);
69
+ }
70
+ async listModels() {
71
+ return this.modelsHandler.execute();
72
+ }
73
+ async embed(request) {
74
+ return this.embeddingHandler.execute(request);
75
+ }
76
+ async transcribe(request) {
77
+ return this.transcriptionHandler.execute(request);
78
+ }
79
+ async moderate(request) {
80
+ return this.moderationHandler.execute(request);
81
+ }
82
+ }
@@ -0,0 +1,8 @@
1
+ import { ModelInfo } from "../Provider.js";
2
+ export declare class MistralModels {
3
+ private readonly baseUrl;
4
+ private readonly apiKey;
5
+ constructor(baseUrl: string, apiKey: string);
6
+ execute(): Promise<ModelInfo[]>;
7
+ }
8
+ //# sourceMappingURL=Models.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Models.d.ts","sourceRoot":"","sources":["../../../src/providers/mistral/Models.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAgB3C,qBAAa,aAAa;IAEtB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM;gBADN,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM;IAG3B,OAAO,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;CAmEtC"}
@@ -0,0 +1,71 @@
1
+ import { MistralCapabilities } from "./Capabilities.js";
2
+ import { ModelRegistry } from "../../models/ModelRegistry.js";
3
+ export class MistralModels {
4
+ baseUrl;
5
+ apiKey;
6
+ constructor(baseUrl, apiKey) {
7
+ this.baseUrl = baseUrl;
8
+ this.apiKey = apiKey;
9
+ }
10
+ async execute() {
11
+ const response = await fetch(`${this.baseUrl}/models`, {
12
+ method: "GET",
13
+ headers: {
14
+ Authorization: `Bearer ${this.apiKey}`,
15
+ Accept: "application/json"
16
+ }
17
+ });
18
+ if (!response.ok) {
19
+ // Fallback to local registry
20
+ const localModels = ModelRegistry.all()
21
+ .filter((m) => m.provider === "mistral")
22
+ .map((m) => ({
23
+ id: m.id,
24
+ name: m.name,
25
+ provider: "mistral",
26
+ family: m.family ?? "mistral",
27
+ context_window: m.context_window ?? null,
28
+ max_output_tokens: m.max_output_tokens ?? null,
29
+ modalities: m.modalities,
30
+ capabilities: m.capabilities,
31
+ pricing: m.pricing,
32
+ metadata: m.metadata
33
+ }));
34
+ return localModels;
35
+ }
36
+ const json = (await response.json());
37
+ const localRegistry = ModelRegistry.all().filter((m) => m.provider === "mistral");
38
+ return json.data.map((m) => {
39
+ // Try to find in local registry for enriched data (pricing, limits)
40
+ const local = localRegistry.find((l) => l.id === m.id);
41
+ if (local) {
42
+ return {
43
+ id: local.id,
44
+ name: local.name,
45
+ provider: "mistral",
46
+ family: local.family ?? "mistral",
47
+ context_window: local.context_window ?? null,
48
+ max_output_tokens: local.max_output_tokens ?? null,
49
+ modalities: local.modalities,
50
+ capabilities: local.capabilities,
51
+ pricing: local.pricing,
52
+ metadata: local.metadata
53
+ };
54
+ }
55
+ return {
56
+ id: m.id,
57
+ name: m.id,
58
+ provider: "mistral",
59
+ family: "mistral",
60
+ context_window: MistralCapabilities.getContextWindow(m.id),
61
+ max_output_tokens: MistralCapabilities.getMaxOutputTokens(m.id),
62
+ modalities: {
63
+ input: MistralCapabilities.getInputModalities(m.id),
64
+ output: MistralCapabilities.getOutputModalities(m.id)
65
+ },
66
+ capabilities: MistralCapabilities.getCapabilities(m.id),
67
+ pricing: MistralCapabilities.getPricing(m.id)
68
+ };
69
+ });
70
+ }
71
+ }