@ai-sdk/moonshotai 2.0.49 → 2.0.51

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.mjs CHANGED
@@ -30,6 +30,39 @@ import {
30
30
  convertBase64ToUint8Array,
31
31
  convertToBase64
32
32
  } from "@ai-sdk/provider-utils";
33
+ var supportedImageMediaTypes = [
34
+ "image/jpeg",
35
+ "image/png",
36
+ "image/gif",
37
+ "image/webp",
38
+ "image/bmp",
39
+ "image/heic",
40
+ "image/heif"
41
+ ];
42
+ var supportedVideoMediaTypes = [
43
+ "video/mp4",
44
+ "video/mpeg",
45
+ "video/mov",
46
+ "video/avi",
47
+ "video/x-flv",
48
+ "video/mpg",
49
+ "video/webm",
50
+ "video/wmv",
51
+ "video/3gpp"
52
+ ];
53
+ function validateMediaType({
54
+ mediaType,
55
+ supportedMediaTypes,
56
+ topLevelMediaType
57
+ }) {
58
+ const normalizedMediaType = mediaType === `${topLevelMediaType}/*` ? topLevelMediaType === "image" ? "image/jpeg" : "video/mp4" : mediaType;
59
+ if (!supportedMediaTypes.includes(normalizedMediaType)) {
60
+ throw new UnsupportedFunctionalityError({
61
+ functionality: `file part media type ${normalizedMediaType}`
62
+ });
63
+ }
64
+ return normalizedMediaType;
65
+ }
33
66
  function convertToMoonshotAIChatMessages(prompt) {
34
67
  var _a;
35
68
  const messages = [];
@@ -56,7 +89,11 @@ function convertToMoonshotAIChatMessages(prompt) {
56
89
  }
57
90
  case "file": {
58
91
  if (part.mediaType.startsWith("image/")) {
59
- const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
92
+ const mediaType = validateMediaType({
93
+ mediaType: part.mediaType,
94
+ supportedMediaTypes: supportedImageMediaTypes,
95
+ topLevelMediaType: "image"
96
+ });
60
97
  return {
61
98
  type: "image_url",
62
99
  image_url: {
@@ -65,7 +102,11 @@ function convertToMoonshotAIChatMessages(prompt) {
65
102
  };
66
103
  }
67
104
  if (part.mediaType.startsWith("video/")) {
68
- const mediaType = part.mediaType === "video/*" ? "video/mp4" : part.mediaType;
105
+ const mediaType = validateMediaType({
106
+ mediaType: part.mediaType,
107
+ supportedMediaTypes: supportedVideoMediaTypes,
108
+ topLevelMediaType: "video"
109
+ });
69
110
  return {
70
111
  type: "video_url",
71
112
  video_url: {
@@ -294,7 +335,7 @@ var moonshotAIChatChunkSchema = lazySchema(
294
335
  reasoning_content: z.string().nullish(),
295
336
  tool_calls: z.array(
296
337
  z.object({
297
- index: z.number(),
338
+ index: z.number().nullish(),
298
339
  id: z.string().nullish(),
299
340
  function: z.object({
300
341
  name: z.string().nullish(),
@@ -303,7 +344,8 @@ var moonshotAIChatChunkSchema = lazySchema(
303
344
  })
304
345
  ).nullish()
305
346
  }).nullish(),
306
- finish_reason: z.string().nullish()
347
+ finish_reason: z.string().nullish(),
348
+ usage: tokenUsageSchema
307
349
  })
308
350
  ),
309
351
  usage: tokenUsageSchema
@@ -315,13 +357,34 @@ var moonshotAIChatChunkSchema = lazySchema(
315
357
 
316
358
  // src/moonshotai-chat-options.ts
317
359
  import { z as z2 } from "zod/v4";
360
+ function isMoonshotAIKimiModel(modelId) {
361
+ return getMoonshotAIModelFamily(modelId).startsWith("kimi-");
362
+ }
363
+ function getMoonshotAIModelFamily(modelId) {
364
+ if (modelId === "kimi-k2.5") return "kimi-k2.5";
365
+ if (modelId === "kimi-k2.6") return "kimi-k2.6";
366
+ if (modelId === "kimi-k2.7-code" || modelId === "kimi-k2.7-code-highspeed") {
367
+ return "kimi-k2.7";
368
+ }
369
+ if (modelId === "kimi-k3") return "kimi-k3";
370
+ if (modelId.startsWith("moonshot-v1-")) return "moonshot-v1";
371
+ return "unknown";
372
+ }
318
373
  var moonshotaiLanguageModelOptions = z2.object({
374
+ /**
375
+ * Whether to use strict JSON schema validation for structured outputs.
376
+ *
377
+ * @default true
378
+ */
379
+ strictJsonSchema: z2.boolean().optional(),
319
380
  /**
320
381
  * Reasoning effort for Kimi K3.
321
382
  */
322
383
  reasoningEffort: z2.enum(["low", "high", "max"]).optional(),
323
384
  thinking: z2.object({
324
385
  type: z2.enum(["enabled", "disabled"]).optional(),
386
+ // Accepted so existing callers receive a migration warning. It remains
387
+ // in the public compatibility type below as a deprecated property.
325
388
  budgetTokens: z2.number().int().min(1024).optional()
326
389
  }).optional(),
327
390
  reasoningHistory: z2.enum(["disabled", "interleaved", "preserved"]).optional(),
@@ -336,14 +399,6 @@ var moonshotaiLanguageModelOptions = z2.object({
336
399
  */
337
400
  safetyIdentifier: z2.string().optional()
338
401
  });
339
- function getModelThinkingKeepSupport(modelId) {
340
- return modelId === "kimi-k2.6" || modelId === "kimi-k3" || modelId.startsWith("kimi-k2.7-code");
341
- }
342
-
343
- // src/moonshotai-prepare-tools.ts
344
- import {
345
- UnsupportedFunctionalityError as UnsupportedFunctionalityError3
346
- } from "@ai-sdk/provider";
347
402
 
348
403
  // src/normalize-json-schema-for-mfjs.ts
349
404
  import { UnsupportedFunctionalityError as UnsupportedFunctionalityError2 } from "@ai-sdk/provider";
@@ -429,9 +484,13 @@ function normalizeDefinition(definition, isRoot) {
429
484
  }
430
485
 
431
486
  // src/moonshotai-prepare-tools.ts
487
+ import {
488
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError3
489
+ } from "@ai-sdk/provider";
432
490
  function prepareTools({
433
491
  tools,
434
- toolChoice
492
+ toolChoice,
493
+ modelId
435
494
  }) {
436
495
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
437
496
  const toolWarnings = [];
@@ -464,7 +523,20 @@ function prepareTools({
464
523
  switch (type) {
465
524
  case "auto":
466
525
  case "none":
526
+ return { tools: moonshotTools, toolChoice: type, toolWarnings };
467
527
  case "required":
528
+ if (modelId === "kimi-k2.6" || modelId === "kimi-k2.7-code" || modelId === "kimi-k2.7-code-highspeed") {
529
+ toolWarnings.push({
530
+ type: "unsupported",
531
+ feature: `tool choice "required" for model "${modelId}"`,
532
+ details: 'Moonshot AI rejects required tool choice for this model. The setting has been omitted; use "auto" or select a specific tool instead.'
533
+ });
534
+ return {
535
+ tools: moonshotTools,
536
+ toolChoice: void 0,
537
+ toolWarnings
538
+ };
539
+ }
468
540
  return { tools: moonshotTools, toolChoice: type, toolWarnings };
469
541
  case "tool":
470
542
  return {
@@ -523,7 +595,7 @@ var MoonshotAIChatLanguageModel = class {
523
595
  toolChoice,
524
596
  tools
525
597
  }) {
526
- var _a, _b;
598
+ var _a, _b, _c;
527
599
  const moonshotOptions = (_a = await parseProviderOptions({
528
600
  provider: this.providerOptionsName,
529
601
  providerOptions,
@@ -537,22 +609,141 @@ var MoonshotAIChatLanguageModel = class {
537
609
  if (seed != null) {
538
610
  allWarnings.push({ type: "unsupported", feature: "seed" });
539
611
  }
612
+ const supportsSamplingOptions = !isMoonshotAIKimiModel(this.modelId);
613
+ if (!supportsSamplingOptions && temperature != null) {
614
+ allWarnings.push({
615
+ type: "unsupported",
616
+ feature: "temperature",
617
+ details: `temperature is fixed by model "${this.modelId}" and has been omitted.`
618
+ });
619
+ }
620
+ if (!supportsSamplingOptions && topP != null) {
621
+ allWarnings.push({
622
+ type: "unsupported",
623
+ feature: "topP",
624
+ details: `topP is fixed by model "${this.modelId}" and has been omitted.`
625
+ });
626
+ }
627
+ if (!supportsSamplingOptions && frequencyPenalty != null) {
628
+ allWarnings.push({
629
+ type: "unsupported",
630
+ feature: "frequencyPenalty",
631
+ details: `frequencyPenalty is fixed by model "${this.modelId}" and has been omitted.`
632
+ });
633
+ }
634
+ if (!supportsSamplingOptions && presencePenalty != null) {
635
+ allWarnings.push({
636
+ type: "unsupported",
637
+ feature: "presencePenalty",
638
+ details: `presencePenalty is fixed by model "${this.modelId}" and has been omitted.`
639
+ });
640
+ }
540
641
  const {
541
642
  tools: moonshotTools,
542
643
  toolChoice: moonshotToolChoice,
543
644
  toolWarnings
544
- } = prepareTools({ tools, toolChoice });
545
- const thinking = moonshotOptions.thinking;
546
- let keep;
547
- if (moonshotOptions.reasoningHistory === "preserved") {
548
- if (getModelThinkingKeepSupport(this.modelId)) {
549
- keep = "all";
550
- } else {
645
+ } = prepareTools({ tools, toolChoice, modelId: this.modelId });
646
+ const modelFamily = getMoonshotAIModelFamily(this.modelId);
647
+ const requestedThinking = moonshotOptions.thinking;
648
+ const requestedReasoningEffort = moonshotOptions.reasoningEffort;
649
+ const preserveReasoning = moonshotOptions.reasoningHistory === "preserved";
650
+ if ((requestedThinking == null ? void 0 : requestedThinking.budgetTokens) != null) {
651
+ allWarnings.push({
652
+ type: "other",
653
+ message: "providerOptions.moonshotai.thinking.budgetTokens is deprecated because Moonshot Chat Completions does not support budget_tokens. The option has been omitted."
654
+ });
655
+ }
656
+ let thinking;
657
+ let reasoningEffort;
658
+ const warnUnsupportedReasoningEffort = () => {
659
+ if (requestedReasoningEffort != null) {
551
660
  allWarnings.push({
552
661
  type: "unsupported",
553
- feature: `reasoningHistory 'preserved' is not supported by model "${this.modelId}"`
662
+ feature: "reasoningEffort",
663
+ details: `reasoningEffort is only supported by Kimi K3 and has been omitted for model "${this.modelId}".`
554
664
  });
555
665
  }
666
+ };
667
+ switch (modelFamily) {
668
+ case "kimi-k3": {
669
+ if (requestedThinking != null) {
670
+ allWarnings.push({
671
+ type: "unsupported",
672
+ feature: "thinking",
673
+ details: "Kimi K3 always reasons and does not accept the thinking field. The option has been omitted."
674
+ });
675
+ }
676
+ reasoningEffort = requestedReasoningEffort;
677
+ break;
678
+ }
679
+ case "kimi-k2.7": {
680
+ warnUnsupportedReasoningEffort();
681
+ if ((requestedThinking == null ? void 0 : requestedThinking.type) === "disabled") {
682
+ allWarnings.push({
683
+ type: "unsupported",
684
+ feature: 'thinking.type "disabled"',
685
+ details: "Kimi K2.7 thinking cannot be disabled."
686
+ });
687
+ } else if ((requestedThinking == null ? void 0 : requestedThinking.type) === "enabled") {
688
+ thinking = { type: "enabled" };
689
+ }
690
+ break;
691
+ }
692
+ case "kimi-k2.6": {
693
+ warnUnsupportedReasoningEffort();
694
+ const thinkingType = requestedThinking == null ? void 0 : requestedThinking.type;
695
+ if (thinkingType != null || preserveReasoning) {
696
+ thinking = {
697
+ type: thinkingType != null ? thinkingType : "enabled",
698
+ ...preserveReasoning ? { keep: "all" } : {}
699
+ };
700
+ }
701
+ break;
702
+ }
703
+ case "kimi-k2.5": {
704
+ warnUnsupportedReasoningEffort();
705
+ const thinkingType = requestedThinking == null ? void 0 : requestedThinking.type;
706
+ if (thinkingType != null) {
707
+ thinking = { type: thinkingType };
708
+ }
709
+ if (preserveReasoning) {
710
+ allWarnings.push({
711
+ type: "unsupported",
712
+ feature: `reasoningHistory 'preserved' is not supported by model "${this.modelId}"`
713
+ });
714
+ }
715
+ break;
716
+ }
717
+ case "moonshot-v1": {
718
+ warnUnsupportedReasoningEffort();
719
+ if (requestedThinking != null) {
720
+ allWarnings.push({
721
+ type: "unsupported",
722
+ feature: "thinking",
723
+ details: `thinking is not supported by model "${this.modelId}" and has been omitted.`
724
+ });
725
+ }
726
+ if (preserveReasoning) {
727
+ allWarnings.push({
728
+ type: "unsupported",
729
+ feature: `reasoningHistory 'preserved' is not supported by model "${this.modelId}"`
730
+ });
731
+ }
732
+ break;
733
+ }
734
+ case "unknown": {
735
+ reasoningEffort = requestedReasoningEffort;
736
+ if ((requestedThinking == null ? void 0 : requestedThinking.type) != null) {
737
+ thinking = { type: requestedThinking.type };
738
+ }
739
+ if (preserveReasoning) {
740
+ allWarnings.push({
741
+ type: "unsupported",
742
+ feature: `reasoningHistory 'preserved' is not supported by model "${this.modelId}"`
743
+ });
744
+ }
745
+ break;
746
+ }
556
747
  }
557
748
  let response_format;
558
749
  if ((responseFormat == null ? void 0 : responseFormat.type) === "json") {
@@ -562,10 +753,8 @@ var MoonshotAIChatLanguageModel = class {
562
753
  type: "json_schema",
563
754
  json_schema: {
564
755
  name: (_b = responseFormat.name) != null ? _b : "response",
565
- schema: schemaWithoutDollarSchema,
566
- ...responseFormat.description != null && {
567
- description: responseFormat.description
568
- }
756
+ strict: (_c = moonshotOptions.strictJsonSchema) != null ? _c : true,
757
+ schema: normalizeJsonSchemaForMFJS(schemaWithoutDollarSchema)
569
758
  }
570
759
  };
571
760
  } else {
@@ -575,27 +764,19 @@ var MoonshotAIChatLanguageModel = class {
575
764
  return {
576
765
  args: {
577
766
  model: this.modelId,
578
- max_tokens: maxOutputTokens,
579
- temperature,
580
- top_p: topP,
581
- frequency_penalty: frequencyPenalty,
582
- presence_penalty: presencePenalty,
767
+ max_completion_tokens: maxOutputTokens,
768
+ temperature: supportsSamplingOptions ? temperature : void 0,
769
+ top_p: supportsSamplingOptions ? topP : void 0,
770
+ frequency_penalty: supportsSamplingOptions ? frequencyPenalty : void 0,
771
+ presence_penalty: supportsSamplingOptions ? presencePenalty : void 0,
583
772
  response_format,
584
773
  stop: stopSequences,
585
774
  messages,
586
775
  tools: moonshotTools,
587
776
  tool_choice: moonshotToolChoice,
588
- ...thinking != null || keep != null ? {
589
- thinking: {
590
- ...(thinking == null ? void 0 : thinking.type) != null && { type: thinking.type },
591
- ...(thinking == null ? void 0 : thinking.budgetTokens) !== void 0 && {
592
- budget_tokens: thinking.budgetTokens
593
- },
594
- ...keep != null && { keep }
595
- }
596
- } : {},
597
- ...moonshotOptions.reasoningEffort != null && {
598
- reasoning_effort: moonshotOptions.reasoningEffort
777
+ ...thinking != null ? { thinking } : {},
778
+ ...reasoningEffort != null && {
779
+ reasoning_effort: reasoningEffort
599
780
  },
600
781
  ...moonshotOptions.promptCacheKey != null && {
601
782
  prompt_cache_key: moonshotOptions.promptCacheKey
@@ -693,7 +874,8 @@ var MoonshotAIChatLanguageModel = class {
693
874
  unified: "other",
694
875
  raw: void 0
695
876
  };
696
- let usage = void 0;
877
+ let topLevelUsage = void 0;
878
+ let choiceUsage = void 0;
697
879
  let isFirstChunk = true;
698
880
  let isActiveReasoning = false;
699
881
  let isActiveText = false;
@@ -704,7 +886,7 @@ var MoonshotAIChatLanguageModel = class {
704
886
  controller.enqueue({ type: "stream-start", warnings });
705
887
  },
706
888
  transform(chunk, controller) {
707
- var _a2, _b2, _c, _d, _e, _f;
889
+ var _a2, _b2, _c, _d, _e, _f, _g;
708
890
  if (options.includeRawChunks) {
709
891
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
710
892
  }
@@ -727,9 +909,12 @@ var MoonshotAIChatLanguageModel = class {
727
909
  });
728
910
  }
729
911
  if (value.usage != null) {
730
- usage = value.usage;
912
+ topLevelUsage = value.usage;
731
913
  }
732
914
  const choice = value.choices[0];
915
+ if ((choice == null ? void 0 : choice.usage) != null) {
916
+ choiceUsage = choice.usage;
917
+ }
733
918
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
734
919
  finishReason = {
735
920
  unified: mapMoonshotAIFinishReason(choice.finish_reason),
@@ -781,8 +966,11 @@ var MoonshotAIChatLanguageModel = class {
781
966
  });
782
967
  isActiveReasoning = false;
783
968
  }
784
- for (const toolCallDelta of delta.tool_calls) {
785
- const index = toolCallDelta.index;
969
+ for (const [
970
+ fallbackIndex,
971
+ toolCallDelta
972
+ ] of delta.tool_calls.entries()) {
973
+ const index = (_a2 = toolCallDelta.index) != null ? _a2 : fallbackIndex;
786
974
  if (toolCalls[index] == null) {
787
975
  if (toolCallDelta.id == null) {
788
976
  throw new InvalidResponseDataError({
@@ -790,7 +978,7 @@ var MoonshotAIChatLanguageModel = class {
790
978
  message: `Expected 'id' to be a string.`
791
979
  });
792
980
  }
793
- if (((_a2 = toolCallDelta.function) == null ? void 0 : _a2.name) == null) {
981
+ if (((_b2 = toolCallDelta.function) == null ? void 0 : _b2.name) == null) {
794
982
  throw new InvalidResponseDataError({
795
983
  data: toolCallDelta,
796
984
  message: `Expected 'function.name' to be a string.`
@@ -806,12 +994,12 @@ var MoonshotAIChatLanguageModel = class {
806
994
  type: "function",
807
995
  function: {
808
996
  name: toolCallDelta.function.name,
809
- arguments: (_b2 = toolCallDelta.function.arguments) != null ? _b2 : ""
997
+ arguments: (_c = toolCallDelta.function.arguments) != null ? _c : ""
810
998
  },
811
999
  hasFinished: false
812
1000
  };
813
1001
  const toolCall2 = toolCalls[index];
814
- if (((_c = toolCall2.function) == null ? void 0 : _c.name) != null && ((_d = toolCall2.function) == null ? void 0 : _d.arguments) != null && toolCall2.function.arguments.length > 0) {
1002
+ if (((_d = toolCall2.function) == null ? void 0 : _d.name) != null && ((_e = toolCall2.function) == null ? void 0 : _e.arguments) != null && toolCall2.function.arguments.length > 0) {
815
1003
  controller.enqueue({
816
1004
  type: "tool-input-delta",
817
1005
  id: toolCall2.id,
@@ -824,13 +1012,13 @@ var MoonshotAIChatLanguageModel = class {
824
1012
  if (toolCall.hasFinished) {
825
1013
  continue;
826
1014
  }
827
- if (((_e = toolCallDelta.function) == null ? void 0 : _e.arguments) != null) {
1015
+ if (((_f = toolCallDelta.function) == null ? void 0 : _f.arguments) != null) {
828
1016
  toolCall.function.arguments += toolCallDelta.function.arguments;
829
1017
  }
830
1018
  controller.enqueue({
831
1019
  type: "tool-input-delta",
832
1020
  id: toolCall.id,
833
- delta: (_f = toolCallDelta.function.arguments) != null ? _f : ""
1021
+ delta: (_g = toolCallDelta.function.arguments) != null ? _g : ""
834
1022
  });
835
1023
  }
836
1024
  }
@@ -859,7 +1047,7 @@ var MoonshotAIChatLanguageModel = class {
859
1047
  controller.enqueue({
860
1048
  type: "finish",
861
1049
  finishReason,
862
- usage: convertMoonshotAIChatUsage(usage)
1050
+ usage: convertMoonshotAIChatUsage(topLevelUsage != null ? topLevelUsage : choiceUsage)
863
1051
  });
864
1052
  }
865
1053
  })
@@ -871,13 +1059,12 @@ var MoonshotAIChatLanguageModel = class {
871
1059
  };
872
1060
 
873
1061
  // src/version.ts
874
- var VERSION = true ? "2.0.49" : "0.0.0-test";
1062
+ var VERSION = true ? "2.0.51" : "0.0.0-test";
875
1063
 
876
1064
  // src/moonshotai-provider.ts
877
1065
  var defaultBaseURL = "https://api.moonshot.ai/v1";
878
1066
  function getModelStructuredOutputSupport(modelId) {
879
- if (modelId.startsWith("kimi-k")) return true;
880
- return false;
1067
+ return modelId.startsWith("kimi-k") || modelId === "moonshot-v1-8k" || modelId === "moonshot-v1-32k" || modelId === "moonshot-v1-128k" || modelId === "moonshot-v1-auto" || modelId === "moonshot-v1-8k-vision-preview" || modelId === "moonshot-v1-32k-vision-preview" || modelId === "moonshot-v1-128k-vision-preview";
881
1068
  }
882
1069
  function createMoonshotAI(options = {}) {
883
1070
  var _a;