@ai-sdk/mistral 4.0.59 → 4.0.60
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/CHANGELOG.md +6 -0
- package/dist/index.d.ts +31 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +744 -4
- package/dist/index.js.map +1 -1
- package/docs/20-mistral.mdx +103 -0
- package/package.json +1 -1
- package/src/convert-to-mistral-conversation-inputs.ts +165 -0
- package/src/index.ts +1 -0
- package/src/mistral-conversation-language-model-options.ts +13 -0
- package/src/mistral-conversation-language-model.ts +730 -0
- package/src/mistral-conversation-prepare-tools.ts +111 -0
- package/src/mistral-provider.ts +19 -0
- package/src/tool/index.ts +3 -0
- package/src/tool/web-search.ts +35 -0
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { InvalidArgumentError, NoSuchModelError, TooManyEmbeddingValuesForCallError, UnsupportedFunctionalityError } from "@ai-sdk/provider";
|
|
2
|
-
import { StreamingToolCallTracker, WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, convertBase64ToUint8Array, convertToBase64, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, createLanguageModelResponseMetadata as getResponseMetadata, createNullLanguageModelUsage, generateId, getTopLevelMediaType, injectJsonInstructionIntoMessages, isCustomReasoning, lazySchema, loadApiKey, mapReasoningToProviderEffort, mediaTypeToExtension, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, resolveFullMediaType, serializeModelOptions, withUserAgentSuffix, withoutTrailingSlash, zodSchema } from "@ai-sdk/provider-utils";
|
|
2
|
+
import { StreamingToolCallTracker, WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, convertBase64ToUint8Array, convertToBase64, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, createLanguageModelResponseMetadata as getResponseMetadata, createNullLanguageModelUsage, createProviderExecutedToolFactory, createToolNameMapping, generateId, getTopLevelMediaType, injectJsonInstructionIntoMessages, isCustomReasoning, lazySchema, loadApiKey, mapReasoningToProviderEffort, mediaTypeToExtension, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, resolveFullMediaType, serializeModelOptions, withUserAgentSuffix, withoutTrailingSlash, zodSchema } from "@ai-sdk/provider-utils";
|
|
3
3
|
import { z } from "zod/v4";
|
|
4
4
|
//#region src/convert-mistral-usage.ts
|
|
5
5
|
const mistralPromptTokensDetailsSchema = z.object({
|
|
@@ -284,7 +284,7 @@ function prepareTools({ tools, toolChoice }) {
|
|
|
284
284
|
}
|
|
285
285
|
//#endregion
|
|
286
286
|
//#region src/mistral-chat-language-model.ts
|
|
287
|
-
const reasoningEffortModelIds = /* @__PURE__ */ new Set([
|
|
287
|
+
const reasoningEffortModelIds$1 = /* @__PURE__ */ new Set([
|
|
288
288
|
"glm-5-2",
|
|
289
289
|
"labs-leanstral-1-5",
|
|
290
290
|
"labs-leanstral-1-5-1",
|
|
@@ -336,7 +336,7 @@ var MistralChatLanguageModel = class MistralChatLanguageModel {
|
|
|
336
336
|
type: "unsupported",
|
|
337
337
|
feature: "topK"
|
|
338
338
|
});
|
|
339
|
-
const supportsReasoningEffort = reasoningEffortModelIds.has(this.modelId);
|
|
339
|
+
const supportsReasoningEffort = reasoningEffortModelIds$1.has(this.modelId);
|
|
340
340
|
let resolvedReasoningEffort;
|
|
341
341
|
if (supportsReasoningEffort) resolvedReasoningEffort = options.reasoningEffort ?? (isCustomReasoning(reasoning) ? reasoning === "none" ? "none" : mapReasoningToProviderEffort({
|
|
342
342
|
reasoning,
|
|
@@ -678,6 +678,713 @@ const mistralChatChunkSchema = z.object({
|
|
|
678
678
|
usage: mistralUsageSchema.nullish()
|
|
679
679
|
});
|
|
680
680
|
//#endregion
|
|
681
|
+
//#region src/convert-to-mistral-conversation-inputs.ts
|
|
682
|
+
function convertToMistralConversationInputs(prompt, toolNameMapping) {
|
|
683
|
+
const inputs = [];
|
|
684
|
+
const instructions = [];
|
|
685
|
+
const executions = /* @__PURE__ */ new Set();
|
|
686
|
+
const appendToolResult = (part) => {
|
|
687
|
+
if (executions.has(part.toolCallId)) {
|
|
688
|
+
let result = null;
|
|
689
|
+
if (part.output.type === "json") {
|
|
690
|
+
const value = part.output.value;
|
|
691
|
+
const info = value != null && typeof value === "object" && "info" in value ? value.info : void 0;
|
|
692
|
+
if (info != null && typeof info === "object" && !Array.isArray(info)) result = "result" in info ? info.result ?? info : info;
|
|
693
|
+
}
|
|
694
|
+
inputs.push({
|
|
695
|
+
type: "function.result",
|
|
696
|
+
tool_call_id: part.toolCallId,
|
|
697
|
+
result: typeof result === "string" ? result : JSON.stringify(result)
|
|
698
|
+
});
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
const converted = convertToMistralChatMessages([{
|
|
702
|
+
role: "tool",
|
|
703
|
+
content: [part]
|
|
704
|
+
}])[0];
|
|
705
|
+
inputs.push({
|
|
706
|
+
type: "function.result",
|
|
707
|
+
tool_call_id: part.toolCallId,
|
|
708
|
+
result: converted.content
|
|
709
|
+
});
|
|
710
|
+
};
|
|
711
|
+
for (const message of prompt) switch (message.role) {
|
|
712
|
+
case "system":
|
|
713
|
+
instructions.push(message.content);
|
|
714
|
+
break;
|
|
715
|
+
case "user": {
|
|
716
|
+
const converted = convertToMistralChatMessages([message])[0];
|
|
717
|
+
if (converted.role === "user") inputs.push({
|
|
718
|
+
type: "message.input",
|
|
719
|
+
role: "user",
|
|
720
|
+
content: converted.content
|
|
721
|
+
});
|
|
722
|
+
break;
|
|
723
|
+
}
|
|
724
|
+
case "assistant": {
|
|
725
|
+
let content = [];
|
|
726
|
+
const flush = () => {
|
|
727
|
+
if (content.length > 0) {
|
|
728
|
+
inputs.push({
|
|
729
|
+
type: "message.input",
|
|
730
|
+
role: "assistant",
|
|
731
|
+
content
|
|
732
|
+
});
|
|
733
|
+
content = [];
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
for (const part of message.content) switch (part.type) {
|
|
737
|
+
case "text":
|
|
738
|
+
content.push({
|
|
739
|
+
type: "text",
|
|
740
|
+
text: part.text
|
|
741
|
+
});
|
|
742
|
+
break;
|
|
743
|
+
case "reasoning":
|
|
744
|
+
content.push({
|
|
745
|
+
type: "thinking",
|
|
746
|
+
thinking: [{
|
|
747
|
+
type: "text",
|
|
748
|
+
text: part.text
|
|
749
|
+
}],
|
|
750
|
+
closed: true
|
|
751
|
+
});
|
|
752
|
+
break;
|
|
753
|
+
case "tool-call":
|
|
754
|
+
flush();
|
|
755
|
+
if (part.providerExecuted) {
|
|
756
|
+
const name = part.providerOptions?.mistral?.function ?? part.providerOptions?.mistral?.name;
|
|
757
|
+
const args = part.input;
|
|
758
|
+
inputs.push({
|
|
759
|
+
type: "function.call",
|
|
760
|
+
tool_call_id: part.toolCallId,
|
|
761
|
+
name: typeof name === "string" ? name : toolNameMapping.toProviderToolName(part.toolName),
|
|
762
|
+
arguments: typeof args?.arguments === "string" ? args.arguments : JSON.stringify(part.input)
|
|
763
|
+
});
|
|
764
|
+
executions.add(part.toolCallId);
|
|
765
|
+
} else inputs.push({
|
|
766
|
+
type: "function.call",
|
|
767
|
+
tool_call_id: part.toolCallId,
|
|
768
|
+
name: part.toolName,
|
|
769
|
+
arguments: JSON.stringify(part.input)
|
|
770
|
+
});
|
|
771
|
+
break;
|
|
772
|
+
case "tool-result":
|
|
773
|
+
flush();
|
|
774
|
+
appendToolResult(part);
|
|
775
|
+
break;
|
|
776
|
+
default: throw new UnsupportedFunctionalityError({ functionality: `assistant content type: ${part.type}` });
|
|
777
|
+
}
|
|
778
|
+
flush();
|
|
779
|
+
break;
|
|
780
|
+
}
|
|
781
|
+
case "tool": for (const part of message.content) {
|
|
782
|
+
if (part.type === "tool-approval-response") continue;
|
|
783
|
+
appendToolResult(part);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
return {
|
|
787
|
+
inputs,
|
|
788
|
+
instructions: instructions.length > 0 ? instructions.join("\n\n") : void 0
|
|
789
|
+
};
|
|
790
|
+
}
|
|
791
|
+
//#endregion
|
|
792
|
+
//#region src/mistral-conversation-language-model-options.ts
|
|
793
|
+
const mistralLanguageModelConversationOptions = mistralLanguageModelChatOptions;
|
|
794
|
+
//#endregion
|
|
795
|
+
//#region src/mistral-conversation-prepare-tools.ts
|
|
796
|
+
const mistralProviderToolNames = {
|
|
797
|
+
"mistral.web_search": "web_search",
|
|
798
|
+
"mistral.web_search_premium": "web_search_premium"
|
|
799
|
+
};
|
|
800
|
+
function prepareConversationTools({ tools, toolChoice }) {
|
|
801
|
+
tools = tools?.length ? tools : void 0;
|
|
802
|
+
const toolWarnings = [];
|
|
803
|
+
if (tools == null) return {
|
|
804
|
+
tools: void 0,
|
|
805
|
+
toolChoice: void 0,
|
|
806
|
+
toolWarnings
|
|
807
|
+
};
|
|
808
|
+
const mistralTools = [];
|
|
809
|
+
for (const tool of tools) if (tool.type === "provider") {
|
|
810
|
+
if (tool.id === "mistral.web_search" || tool.id === "mistral.web_search_premium") {
|
|
811
|
+
mistralTools.push({ type: mistralProviderToolNames[tool.id] });
|
|
812
|
+
continue;
|
|
813
|
+
}
|
|
814
|
+
toolWarnings.push({
|
|
815
|
+
type: "unsupported",
|
|
816
|
+
feature: `provider-defined tool ${tool.id}`
|
|
817
|
+
});
|
|
818
|
+
} else mistralTools.push({
|
|
819
|
+
type: "function",
|
|
820
|
+
function: {
|
|
821
|
+
name: tool.name,
|
|
822
|
+
description: tool.description,
|
|
823
|
+
parameters: tool.inputSchema,
|
|
824
|
+
...tool.strict != null ? { strict: tool.strict } : {}
|
|
825
|
+
}
|
|
826
|
+
});
|
|
827
|
+
if (toolChoice == null) return {
|
|
828
|
+
tools: mistralTools,
|
|
829
|
+
toolChoice: void 0,
|
|
830
|
+
toolWarnings
|
|
831
|
+
};
|
|
832
|
+
const type = toolChoice.type;
|
|
833
|
+
switch (type) {
|
|
834
|
+
case "auto":
|
|
835
|
+
case "none": return {
|
|
836
|
+
tools: mistralTools,
|
|
837
|
+
toolChoice: type,
|
|
838
|
+
toolWarnings
|
|
839
|
+
};
|
|
840
|
+
case "required": return {
|
|
841
|
+
tools: mistralTools,
|
|
842
|
+
toolChoice: "any",
|
|
843
|
+
toolWarnings
|
|
844
|
+
};
|
|
845
|
+
case "tool": {
|
|
846
|
+
const toolName = createToolNameMapping({
|
|
847
|
+
tools,
|
|
848
|
+
providerToolNames: mistralProviderToolNames
|
|
849
|
+
}).toProviderToolName(toolChoice.toolName);
|
|
850
|
+
return {
|
|
851
|
+
tools: mistralTools.filter((tool) => tool.type === "function" ? tool.function.name === toolName : tool.type === toolName),
|
|
852
|
+
toolChoice: "any",
|
|
853
|
+
toolWarnings
|
|
854
|
+
};
|
|
855
|
+
}
|
|
856
|
+
default: throw new UnsupportedFunctionalityError({ functionality: `tool choice type: ${type}` });
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
//#endregion
|
|
860
|
+
//#region src/mistral-conversation-language-model.ts
|
|
861
|
+
const reasoningEffortModelIds = /* @__PURE__ */ new Set([
|
|
862
|
+
"glm-5-2",
|
|
863
|
+
"labs-leanstral-1-5",
|
|
864
|
+
"labs-leanstral-1-5-1",
|
|
865
|
+
"magistral-medium-latest",
|
|
866
|
+
"magistral-small-latest",
|
|
867
|
+
"mistral-large-4",
|
|
868
|
+
"mistral-large-4-0",
|
|
869
|
+
"mistral-medium",
|
|
870
|
+
"mistral-medium-2604",
|
|
871
|
+
"mistral-medium-3",
|
|
872
|
+
"mistral-medium-3-5",
|
|
873
|
+
"mistral-medium-3.5",
|
|
874
|
+
"mistral-medium-latest",
|
|
875
|
+
"mistral-small-2603",
|
|
876
|
+
"mistral-small-latest",
|
|
877
|
+
"mistral-vibe-cli-fast",
|
|
878
|
+
"mistral-vibe-cli-latest",
|
|
879
|
+
"mistral-vibe-cli-with-tools",
|
|
880
|
+
"zai-glm-5-2"
|
|
881
|
+
]);
|
|
882
|
+
var MistralConversationLanguageModel = class MistralConversationLanguageModel {
|
|
883
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
884
|
+
return serializeModelOptions({
|
|
885
|
+
modelId: model.modelId,
|
|
886
|
+
config: model.config
|
|
887
|
+
});
|
|
888
|
+
}
|
|
889
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
890
|
+
return new MistralConversationLanguageModel(options.modelId, options.config);
|
|
891
|
+
}
|
|
892
|
+
constructor(modelId, config) {
|
|
893
|
+
this.specificationVersion = "v4";
|
|
894
|
+
this.supportedUrls = { "application/pdf": [/^https:\/\/.*$/] };
|
|
895
|
+
this.modelId = modelId;
|
|
896
|
+
this.config = config;
|
|
897
|
+
this.generateId = config.generateId ?? generateId;
|
|
898
|
+
}
|
|
899
|
+
get provider() {
|
|
900
|
+
return this.config.provider;
|
|
901
|
+
}
|
|
902
|
+
async getArgs({ prompt, maxOutputTokens, temperature, topP, topK, frequencyPenalty, presencePenalty, reasoning, stopSequences, responseFormat, seed, providerOptions, tools, toolChoice }) {
|
|
903
|
+
const warnings = [];
|
|
904
|
+
const options = await parseProviderOptions({
|
|
905
|
+
provider: "mistral",
|
|
906
|
+
providerOptions,
|
|
907
|
+
schema: mistralLanguageModelConversationOptions
|
|
908
|
+
}) ?? {};
|
|
909
|
+
if (topK != null) warnings.push({
|
|
910
|
+
type: "unsupported",
|
|
911
|
+
feature: "topK"
|
|
912
|
+
});
|
|
913
|
+
const supportsReasoningEffort = reasoningEffortModelIds.has(this.modelId);
|
|
914
|
+
let resolvedReasoningEffort;
|
|
915
|
+
if (supportsReasoningEffort) resolvedReasoningEffort = options.reasoningEffort ?? (isCustomReasoning(reasoning) ? reasoning === "none" ? "none" : mapReasoningToProviderEffort({
|
|
916
|
+
reasoning,
|
|
917
|
+
effortMap: {
|
|
918
|
+
minimal: "high",
|
|
919
|
+
low: "high",
|
|
920
|
+
medium: "high",
|
|
921
|
+
high: "high",
|
|
922
|
+
xhigh: "high",
|
|
923
|
+
max: "high"
|
|
924
|
+
},
|
|
925
|
+
warnings
|
|
926
|
+
}) : void 0);
|
|
927
|
+
else if (isCustomReasoning(reasoning)) warnings.push({
|
|
928
|
+
type: "unsupported",
|
|
929
|
+
feature: "reasoning",
|
|
930
|
+
details: "This model does not support reasoning configuration."
|
|
931
|
+
});
|
|
932
|
+
const structuredOutputs = options.structuredOutputs ?? true;
|
|
933
|
+
const strictJsonSchema = options.strictJsonSchema ?? false;
|
|
934
|
+
if (responseFormat?.type === "json" && (!structuredOutputs || responseFormat.schema == null)) prompt = injectJsonInstructionIntoMessages({
|
|
935
|
+
messages: prompt,
|
|
936
|
+
schema: responseFormat.schema
|
|
937
|
+
});
|
|
938
|
+
const baseArgs = {
|
|
939
|
+
max_tokens: maxOutputTokens,
|
|
940
|
+
temperature,
|
|
941
|
+
top_p: topP,
|
|
942
|
+
...frequencyPenalty != null ? { frequency_penalty: frequencyPenalty } : {},
|
|
943
|
+
...presencePenalty != null ? { presence_penalty: presencePenalty } : {},
|
|
944
|
+
stop: stopSequences,
|
|
945
|
+
random_seed: seed,
|
|
946
|
+
reasoning_effort: resolvedReasoningEffort,
|
|
947
|
+
response_format: responseFormat?.type === "json" ? structuredOutputs && responseFormat?.schema != null ? {
|
|
948
|
+
type: "json_schema",
|
|
949
|
+
json_schema: {
|
|
950
|
+
schema: responseFormat.schema,
|
|
951
|
+
strict: strictJsonSchema,
|
|
952
|
+
name: responseFormat.name ?? "response",
|
|
953
|
+
description: responseFormat.description
|
|
954
|
+
}
|
|
955
|
+
} : { type: "json_object" } : void 0
|
|
956
|
+
};
|
|
957
|
+
const { tools: mistralTools, toolChoice: mistralToolChoice, toolWarnings } = prepareConversationTools({
|
|
958
|
+
tools,
|
|
959
|
+
toolChoice
|
|
960
|
+
});
|
|
961
|
+
for (const [feature, value] of Object.entries({
|
|
962
|
+
safePrompt: options.safePrompt,
|
|
963
|
+
documentImageLimit: options.documentImageLimit,
|
|
964
|
+
documentPageLimit: options.documentPageLimit,
|
|
965
|
+
promptCacheKey: options.promptCacheKey,
|
|
966
|
+
parallelToolCalls: options.parallelToolCalls
|
|
967
|
+
})) if (value != null) warnings.push({
|
|
968
|
+
type: "unsupported",
|
|
969
|
+
feature,
|
|
970
|
+
details: "Not supported by the Mistral Conversations API."
|
|
971
|
+
});
|
|
972
|
+
return {
|
|
973
|
+
args: {
|
|
974
|
+
model: this.modelId,
|
|
975
|
+
store: false,
|
|
976
|
+
...convertToMistralConversationInputs(prompt, createToolNameMapping({
|
|
977
|
+
tools,
|
|
978
|
+
providerToolNames: mistralProviderToolNames
|
|
979
|
+
})),
|
|
980
|
+
tools: mistralTools,
|
|
981
|
+
completion_args: {
|
|
982
|
+
...baseArgs,
|
|
983
|
+
tool_choice: mistralToolChoice
|
|
984
|
+
}
|
|
985
|
+
},
|
|
986
|
+
warnings: [...warnings, ...toolWarnings]
|
|
987
|
+
};
|
|
988
|
+
}
|
|
989
|
+
async doGenerate(options) {
|
|
990
|
+
const { args: body, warnings } = await this.getArgs(options);
|
|
991
|
+
const { value: response, rawValue: rawResponse, responseHeaders } = await postJsonToApi({
|
|
992
|
+
url: `${this.config.baseURL}/conversations`,
|
|
993
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
994
|
+
body,
|
|
995
|
+
failedResponseHandler: mistralFailedResponseHandler,
|
|
996
|
+
successfulResponseHandler: createJsonResponseHandler(mistralConversationResponseSchema),
|
|
997
|
+
abortSignal: options.abortSignal,
|
|
998
|
+
fetch: this.config.fetch
|
|
999
|
+
});
|
|
1000
|
+
const converter = createContentConverter({
|
|
1001
|
+
tools: options.tools,
|
|
1002
|
+
generateId: this.generateId
|
|
1003
|
+
});
|
|
1004
|
+
const content = [];
|
|
1005
|
+
let hasFunctionCalls = false;
|
|
1006
|
+
let modelId;
|
|
1007
|
+
for (const output of response.outputs) switch (output.type) {
|
|
1008
|
+
case "tool.execution":
|
|
1009
|
+
content.push(...converter.convertTool(output));
|
|
1010
|
+
break;
|
|
1011
|
+
case "function.call":
|
|
1012
|
+
hasFunctionCalls = true;
|
|
1013
|
+
content.push({
|
|
1014
|
+
type: "tool-call",
|
|
1015
|
+
toolCallId: output.tool_call_id,
|
|
1016
|
+
toolName: output.name,
|
|
1017
|
+
input: typeof output.arguments === "string" ? output.arguments : JSON.stringify(output.arguments)
|
|
1018
|
+
});
|
|
1019
|
+
break;
|
|
1020
|
+
case "message.output":
|
|
1021
|
+
modelId = output.model ?? modelId;
|
|
1022
|
+
for (const chunk of typeof output.content === "string" ? [{
|
|
1023
|
+
type: "text",
|
|
1024
|
+
text: output.content
|
|
1025
|
+
}] : output.content) content.push(...converter.convertChunk(chunk));
|
|
1026
|
+
}
|
|
1027
|
+
return {
|
|
1028
|
+
content,
|
|
1029
|
+
finishReason: {
|
|
1030
|
+
unified: hasFunctionCalls ? "tool-calls" : "stop",
|
|
1031
|
+
raw: void 0
|
|
1032
|
+
},
|
|
1033
|
+
usage: convertMistralUsage(response.usage),
|
|
1034
|
+
request: { body },
|
|
1035
|
+
response: {
|
|
1036
|
+
id: response.conversation_id,
|
|
1037
|
+
modelId,
|
|
1038
|
+
headers: responseHeaders,
|
|
1039
|
+
body: rawResponse
|
|
1040
|
+
},
|
|
1041
|
+
warnings
|
|
1042
|
+
};
|
|
1043
|
+
}
|
|
1044
|
+
async doStream(options) {
|
|
1045
|
+
const { args, warnings } = await this.getArgs(options);
|
|
1046
|
+
const body = {
|
|
1047
|
+
...args,
|
|
1048
|
+
stream: true
|
|
1049
|
+
};
|
|
1050
|
+
const { value: response, responseHeaders } = await postJsonToApi({
|
|
1051
|
+
url: `${this.config.baseURL}/conversations`,
|
|
1052
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
1053
|
+
body,
|
|
1054
|
+
failedResponseHandler: mistralFailedResponseHandler,
|
|
1055
|
+
successfulResponseHandler: createEventSourceResponseHandler(mistralConversationChunkSchema),
|
|
1056
|
+
abortSignal: options.abortSignal,
|
|
1057
|
+
fetch: this.config.fetch
|
|
1058
|
+
});
|
|
1059
|
+
const converter = createContentConverter({
|
|
1060
|
+
tools: options.tools,
|
|
1061
|
+
generateId: this.generateId
|
|
1062
|
+
});
|
|
1063
|
+
const executions = /* @__PURE__ */ new Map();
|
|
1064
|
+
let toolCallTracker;
|
|
1065
|
+
let active;
|
|
1066
|
+
let usage;
|
|
1067
|
+
let hasFunctionCalls = false;
|
|
1068
|
+
let hasError = false;
|
|
1069
|
+
let isDone = false;
|
|
1070
|
+
let hasModelId = false;
|
|
1071
|
+
const closeContent = (controller) => {
|
|
1072
|
+
if (active != null) {
|
|
1073
|
+
controller.enqueue({
|
|
1074
|
+
type: active.type === "text" ? "text-end" : "reasoning-end",
|
|
1075
|
+
id: active.id
|
|
1076
|
+
});
|
|
1077
|
+
active = void 0;
|
|
1078
|
+
}
|
|
1079
|
+
};
|
|
1080
|
+
const generateId = this.generateId;
|
|
1081
|
+
return {
|
|
1082
|
+
stream: response.pipeThrough(new TransformStream({
|
|
1083
|
+
start(controller) {
|
|
1084
|
+
toolCallTracker = new StreamingToolCallTracker(controller, { generateId });
|
|
1085
|
+
controller.enqueue({
|
|
1086
|
+
type: "stream-start",
|
|
1087
|
+
warnings
|
|
1088
|
+
});
|
|
1089
|
+
},
|
|
1090
|
+
transform(chunk, controller) {
|
|
1091
|
+
if (options.includeRawChunks) controller.enqueue({
|
|
1092
|
+
type: "raw",
|
|
1093
|
+
rawValue: chunk.rawValue
|
|
1094
|
+
});
|
|
1095
|
+
if (!chunk.success) {
|
|
1096
|
+
hasError = true;
|
|
1097
|
+
controller.enqueue({
|
|
1098
|
+
type: "error",
|
|
1099
|
+
error: chunk.error
|
|
1100
|
+
});
|
|
1101
|
+
return;
|
|
1102
|
+
}
|
|
1103
|
+
const event = chunk.value;
|
|
1104
|
+
switch (event.type) {
|
|
1105
|
+
case "conversation.response.started":
|
|
1106
|
+
controller.enqueue({
|
|
1107
|
+
type: "response-metadata",
|
|
1108
|
+
id: event.conversation_id,
|
|
1109
|
+
...event.created_at != null ? { timestamp: new Date(event.created_at) } : {}
|
|
1110
|
+
});
|
|
1111
|
+
break;
|
|
1112
|
+
case "conversation.response.done":
|
|
1113
|
+
usage = event.usage ?? void 0;
|
|
1114
|
+
isDone = true;
|
|
1115
|
+
break;
|
|
1116
|
+
case "conversation.response.error":
|
|
1117
|
+
hasError = true;
|
|
1118
|
+
controller.enqueue({
|
|
1119
|
+
type: "error",
|
|
1120
|
+
error: event
|
|
1121
|
+
});
|
|
1122
|
+
break;
|
|
1123
|
+
case "message.output.delta":
|
|
1124
|
+
if (!hasModelId && event.model != null) {
|
|
1125
|
+
hasModelId = true;
|
|
1126
|
+
controller.enqueue({
|
|
1127
|
+
type: "response-metadata",
|
|
1128
|
+
modelId: event.model
|
|
1129
|
+
});
|
|
1130
|
+
}
|
|
1131
|
+
for (const part of converter.convertChunk(typeof event.content === "string" ? {
|
|
1132
|
+
type: "text",
|
|
1133
|
+
text: event.content
|
|
1134
|
+
} : event.content)) if (part.type === "text" || part.type === "reasoning") {
|
|
1135
|
+
const id = `${event.id}-${event.content_index ?? 0}`;
|
|
1136
|
+
if (active?.id !== id || active.type !== part.type) {
|
|
1137
|
+
closeContent(controller);
|
|
1138
|
+
active = {
|
|
1139
|
+
id,
|
|
1140
|
+
type: part.type
|
|
1141
|
+
};
|
|
1142
|
+
controller.enqueue({
|
|
1143
|
+
type: part.type === "text" ? "text-start" : "reasoning-start",
|
|
1144
|
+
id
|
|
1145
|
+
});
|
|
1146
|
+
}
|
|
1147
|
+
controller.enqueue({
|
|
1148
|
+
type: part.type === "text" ? "text-delta" : "reasoning-delta",
|
|
1149
|
+
id,
|
|
1150
|
+
delta: part.text
|
|
1151
|
+
});
|
|
1152
|
+
} else if (part.type === "source") controller.enqueue(part);
|
|
1153
|
+
break;
|
|
1154
|
+
case "function.call.delta":
|
|
1155
|
+
closeContent(controller);
|
|
1156
|
+
hasFunctionCalls = true;
|
|
1157
|
+
toolCallTracker.processDelta({
|
|
1158
|
+
id: event.tool_call_id,
|
|
1159
|
+
index: event.output_index,
|
|
1160
|
+
function: {
|
|
1161
|
+
name: event.name,
|
|
1162
|
+
arguments: event.arguments
|
|
1163
|
+
}
|
|
1164
|
+
});
|
|
1165
|
+
break;
|
|
1166
|
+
case "tool.execution.started":
|
|
1167
|
+
case "tool.execution.delta": {
|
|
1168
|
+
closeContent(controller);
|
|
1169
|
+
const execution = executions.get(event.id);
|
|
1170
|
+
if (execution == null) {
|
|
1171
|
+
executions.set(event.id, {
|
|
1172
|
+
name: event.name,
|
|
1173
|
+
arguments: event.arguments,
|
|
1174
|
+
function: event.function
|
|
1175
|
+
});
|
|
1176
|
+
controller.enqueue({
|
|
1177
|
+
type: "tool-input-start",
|
|
1178
|
+
id: event.id,
|
|
1179
|
+
toolName: converter.toolNameMapping.toCustomToolName(event.name),
|
|
1180
|
+
providerExecuted: true
|
|
1181
|
+
});
|
|
1182
|
+
} else execution.arguments += event.arguments;
|
|
1183
|
+
break;
|
|
1184
|
+
}
|
|
1185
|
+
case "tool.execution.done": {
|
|
1186
|
+
const execution = executions.get(event.id);
|
|
1187
|
+
if (execution == null) {
|
|
1188
|
+
hasError = true;
|
|
1189
|
+
controller.enqueue({
|
|
1190
|
+
type: "error",
|
|
1191
|
+
error: /* @__PURE__ */ new Error(`Missing tool execution start for ${event.id}`)
|
|
1192
|
+
});
|
|
1193
|
+
break;
|
|
1194
|
+
}
|
|
1195
|
+
controller.enqueue({
|
|
1196
|
+
type: "tool-input-delta",
|
|
1197
|
+
id: event.id,
|
|
1198
|
+
delta: JSON.stringify({ arguments: execution.arguments })
|
|
1199
|
+
});
|
|
1200
|
+
controller.enqueue({
|
|
1201
|
+
type: "tool-input-end",
|
|
1202
|
+
id: event.id
|
|
1203
|
+
});
|
|
1204
|
+
for (const part of converter.convertTool({
|
|
1205
|
+
type: "tool.execution",
|
|
1206
|
+
id: event.id,
|
|
1207
|
+
name: execution.name,
|
|
1208
|
+
arguments: execution.arguments,
|
|
1209
|
+
function: event.function ?? execution.function,
|
|
1210
|
+
info: event.info
|
|
1211
|
+
})) controller.enqueue(part);
|
|
1212
|
+
executions.delete(event.id);
|
|
1213
|
+
break;
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
},
|
|
1217
|
+
flush(controller) {
|
|
1218
|
+
closeContent(controller);
|
|
1219
|
+
toolCallTracker.flush();
|
|
1220
|
+
controller.enqueue({
|
|
1221
|
+
type: "finish",
|
|
1222
|
+
finishReason: {
|
|
1223
|
+
unified: hasError || !isDone || executions.size > 0 ? "error" : hasFunctionCalls ? "tool-calls" : "stop",
|
|
1224
|
+
raw: void 0
|
|
1225
|
+
},
|
|
1226
|
+
usage: convertMistralUsage(usage)
|
|
1227
|
+
});
|
|
1228
|
+
}
|
|
1229
|
+
})),
|
|
1230
|
+
request: { body },
|
|
1231
|
+
response: { headers: responseHeaders }
|
|
1232
|
+
};
|
|
1233
|
+
}
|
|
1234
|
+
};
|
|
1235
|
+
function createContentConverter({ tools, generateId }) {
|
|
1236
|
+
const toolNameMapping = createToolNameMapping({
|
|
1237
|
+
tools,
|
|
1238
|
+
providerToolNames: mistralProviderToolNames
|
|
1239
|
+
});
|
|
1240
|
+
const sourceUrls = /* @__PURE__ */ new Set();
|
|
1241
|
+
return {
|
|
1242
|
+
toolNameMapping,
|
|
1243
|
+
convertChunk(chunk) {
|
|
1244
|
+
switch (chunk.type) {
|
|
1245
|
+
case "text": return chunk.text.length > 0 ? [{
|
|
1246
|
+
type: "text",
|
|
1247
|
+
text: chunk.text
|
|
1248
|
+
}] : [];
|
|
1249
|
+
case "thinking": {
|
|
1250
|
+
const text = chunk.thinking.map((part) => part.text).join("");
|
|
1251
|
+
return text.length > 0 ? [{
|
|
1252
|
+
type: "reasoning",
|
|
1253
|
+
text
|
|
1254
|
+
}] : [];
|
|
1255
|
+
}
|
|
1256
|
+
case "tool_reference":
|
|
1257
|
+
if (chunk.url == null || sourceUrls.has(chunk.url)) return [];
|
|
1258
|
+
const id = generateId();
|
|
1259
|
+
sourceUrls.add(chunk.url);
|
|
1260
|
+
return [{
|
|
1261
|
+
type: "source",
|
|
1262
|
+
sourceType: "url",
|
|
1263
|
+
id,
|
|
1264
|
+
url: chunk.url,
|
|
1265
|
+
title: chunk.title ?? chunk.url
|
|
1266
|
+
}];
|
|
1267
|
+
}
|
|
1268
|
+
},
|
|
1269
|
+
convertTool(output) {
|
|
1270
|
+
const toolName = toolNameMapping.toCustomToolName(output.name);
|
|
1271
|
+
return [{
|
|
1272
|
+
type: "tool-call",
|
|
1273
|
+
toolCallId: output.id,
|
|
1274
|
+
toolName,
|
|
1275
|
+
input: JSON.stringify({ arguments: output.arguments }),
|
|
1276
|
+
providerExecuted: true,
|
|
1277
|
+
providerMetadata: { mistral: {
|
|
1278
|
+
type: "tool.execution",
|
|
1279
|
+
name: output.name,
|
|
1280
|
+
...output.function != null ? { function: output.function } : {}
|
|
1281
|
+
} }
|
|
1282
|
+
}, {
|
|
1283
|
+
type: "tool-result",
|
|
1284
|
+
toolCallId: output.id,
|
|
1285
|
+
toolName,
|
|
1286
|
+
result: { ...output.info != null ? { info: output.info } : {} }
|
|
1287
|
+
}];
|
|
1288
|
+
}
|
|
1289
|
+
};
|
|
1290
|
+
}
|
|
1291
|
+
const mistralConversationContentSchema = z.discriminatedUnion("type", [
|
|
1292
|
+
z.object({
|
|
1293
|
+
type: z.literal("text"),
|
|
1294
|
+
text: z.string()
|
|
1295
|
+
}),
|
|
1296
|
+
z.object({
|
|
1297
|
+
type: z.literal("thinking"),
|
|
1298
|
+
thinking: z.array(z.object({
|
|
1299
|
+
type: z.literal("text"),
|
|
1300
|
+
text: z.string()
|
|
1301
|
+
}))
|
|
1302
|
+
}),
|
|
1303
|
+
z.object({
|
|
1304
|
+
type: z.literal("tool_reference"),
|
|
1305
|
+
tool: z.string().nullish(),
|
|
1306
|
+
title: z.string().nullish(),
|
|
1307
|
+
url: z.string().nullish()
|
|
1308
|
+
})
|
|
1309
|
+
]);
|
|
1310
|
+
const mistralConversationMessageContentSchema = z.union([z.string(), z.array(mistralConversationContentSchema)]);
|
|
1311
|
+
const mistralConversationToolExecutionSchema = z.object({
|
|
1312
|
+
type: z.literal("tool.execution"),
|
|
1313
|
+
id: z.string(),
|
|
1314
|
+
name: z.string(),
|
|
1315
|
+
arguments: z.string(),
|
|
1316
|
+
function: z.string().nullish(),
|
|
1317
|
+
info: z.record(z.string(), z.json()).nullish()
|
|
1318
|
+
});
|
|
1319
|
+
const mistralConversationFunctionCallSchema = z.object({
|
|
1320
|
+
type: z.literal("function.call"),
|
|
1321
|
+
tool_call_id: z.string(),
|
|
1322
|
+
name: z.string(),
|
|
1323
|
+
arguments: z.union([z.string(), z.record(z.string(), z.json())])
|
|
1324
|
+
});
|
|
1325
|
+
const mistralConversationUsageSchema = mistralUsageSchema.extend({
|
|
1326
|
+
prompt_tokens: z.number().default(0),
|
|
1327
|
+
completion_tokens: z.number().default(0),
|
|
1328
|
+
total_tokens: z.number().default(0)
|
|
1329
|
+
});
|
|
1330
|
+
const mistralConversationResponseSchema = z.object({
|
|
1331
|
+
conversation_id: z.string(),
|
|
1332
|
+
outputs: z.array(z.discriminatedUnion("type", [
|
|
1333
|
+
mistralConversationToolExecutionSchema,
|
|
1334
|
+
mistralConversationFunctionCallSchema,
|
|
1335
|
+
z.object({
|
|
1336
|
+
type: z.literal("message.output"),
|
|
1337
|
+
content: mistralConversationMessageContentSchema,
|
|
1338
|
+
model: z.string().nullish()
|
|
1339
|
+
})
|
|
1340
|
+
])),
|
|
1341
|
+
usage: mistralConversationUsageSchema.nullish()
|
|
1342
|
+
});
|
|
1343
|
+
const mistralConversationChunkSchema = z.discriminatedUnion("type", [
|
|
1344
|
+
z.object({
|
|
1345
|
+
type: z.literal("conversation.response.started"),
|
|
1346
|
+
conversation_id: z.string(),
|
|
1347
|
+
created_at: z.string().nullish()
|
|
1348
|
+
}),
|
|
1349
|
+
z.object({
|
|
1350
|
+
type: z.literal("conversation.response.done"),
|
|
1351
|
+
usage: mistralConversationUsageSchema.nullish()
|
|
1352
|
+
}),
|
|
1353
|
+
z.object({
|
|
1354
|
+
type: z.literal("conversation.response.error"),
|
|
1355
|
+
message: z.string(),
|
|
1356
|
+
code: z.number()
|
|
1357
|
+
}),
|
|
1358
|
+
z.object({
|
|
1359
|
+
type: z.literal("message.output.delta"),
|
|
1360
|
+
id: z.string(),
|
|
1361
|
+
content_index: z.number().nullish(),
|
|
1362
|
+
content: z.union([z.string(), mistralConversationContentSchema]),
|
|
1363
|
+
model: z.string().nullish()
|
|
1364
|
+
}),
|
|
1365
|
+
z.object({
|
|
1366
|
+
type: z.literal("function.call.delta"),
|
|
1367
|
+
tool_call_id: z.string(),
|
|
1368
|
+
name: z.string(),
|
|
1369
|
+
arguments: z.string(),
|
|
1370
|
+
output_index: z.number().nullish()
|
|
1371
|
+
}),
|
|
1372
|
+
z.object({
|
|
1373
|
+
type: z.enum(["tool.execution.started", "tool.execution.delta"]),
|
|
1374
|
+
id: z.string(),
|
|
1375
|
+
name: z.string(),
|
|
1376
|
+
arguments: z.string(),
|
|
1377
|
+
function: z.string().nullish()
|
|
1378
|
+
}),
|
|
1379
|
+
z.object({
|
|
1380
|
+
type: z.literal("tool.execution.done"),
|
|
1381
|
+
id: z.string(),
|
|
1382
|
+
name: z.string(),
|
|
1383
|
+
function: z.string().nullish(),
|
|
1384
|
+
info: z.record(z.string(), z.json()).nullish()
|
|
1385
|
+
})
|
|
1386
|
+
]);
|
|
1387
|
+
//#endregion
|
|
681
1388
|
//#region src/mistral-embedding-model-options.ts
|
|
682
1389
|
const mistralEmbeddingModelOptions = z.object({
|
|
683
1390
|
/**
|
|
@@ -1032,7 +1739,31 @@ const mistralTranscriptionResponseSchema = z.object({
|
|
|
1032
1739
|
});
|
|
1033
1740
|
//#endregion
|
|
1034
1741
|
//#region src/version.ts
|
|
1035
|
-
const VERSION = "4.0.
|
|
1742
|
+
const VERSION = "4.0.60";
|
|
1743
|
+
//#endregion
|
|
1744
|
+
//#region src/tool/web-search.ts
|
|
1745
|
+
const inputSchema = lazySchema(() => zodSchema(z.object({ arguments: z.string() })));
|
|
1746
|
+
const outputSchema = lazySchema(() => zodSchema(z.object({ info: z.record(z.string(), z.json()).optional() })));
|
|
1747
|
+
const webSearchFactory = createProviderExecutedToolFactory({
|
|
1748
|
+
id: "mistral.web_search",
|
|
1749
|
+
inputSchema,
|
|
1750
|
+
outputSchema
|
|
1751
|
+
});
|
|
1752
|
+
const webSearchPremiumFactory = createProviderExecutedToolFactory({
|
|
1753
|
+
id: "mistral.web_search_premium",
|
|
1754
|
+
inputSchema,
|
|
1755
|
+
outputSchema
|
|
1756
|
+
});
|
|
1757
|
+
/** Search the web using Mistral's Conversations API. */
|
|
1758
|
+
const webSearch = (args = {}) => webSearchFactory(args);
|
|
1759
|
+
/** Search the web and verified news using Mistral's Conversations API. */
|
|
1760
|
+
const webSearchPremium = (args = {}) => webSearchPremiumFactory(args);
|
|
1761
|
+
//#endregion
|
|
1762
|
+
//#region src/tool/index.ts
|
|
1763
|
+
const mistralTools = {
|
|
1764
|
+
webSearch,
|
|
1765
|
+
webSearchPremium
|
|
1766
|
+
};
|
|
1036
1767
|
//#endregion
|
|
1037
1768
|
//#region src/mistral-provider.ts
|
|
1038
1769
|
/**
|
|
@@ -1061,6 +1792,13 @@ function createMistral(options = {}) {
|
|
|
1061
1792
|
headers: getHeaders,
|
|
1062
1793
|
fetch: options.fetch
|
|
1063
1794
|
});
|
|
1795
|
+
const createConversationModel = (modelId) => new MistralConversationLanguageModel(modelId, {
|
|
1796
|
+
provider: "mistral.conversation",
|
|
1797
|
+
baseURL,
|
|
1798
|
+
headers: getHeaders,
|
|
1799
|
+
fetch: options.fetch,
|
|
1800
|
+
generateId: options.generateId
|
|
1801
|
+
});
|
|
1064
1802
|
const createSpeechModel = (modelId) => new MistralSpeechModel(modelId, {
|
|
1065
1803
|
provider: "mistral.speech",
|
|
1066
1804
|
baseURL,
|
|
@@ -1080,6 +1818,8 @@ function createMistral(options = {}) {
|
|
|
1080
1818
|
provider.specificationVersion = "v4";
|
|
1081
1819
|
provider.languageModel = createChatModel;
|
|
1082
1820
|
provider.chat = createChatModel;
|
|
1821
|
+
provider.conversation = createConversationModel;
|
|
1822
|
+
provider.tools = mistralTools;
|
|
1083
1823
|
provider.embedding = createEmbeddingModel;
|
|
1084
1824
|
provider.embeddingModel = createEmbeddingModel;
|
|
1085
1825
|
provider.textEmbedding = createEmbeddingModel;
|