@ai-sdk/moonshotai 2.0.49 → 2.0.53

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
@@ -24,26 +24,402 @@ import {
24
24
 
25
25
  // src/convert-to-moonshotai-chat-messages.ts
26
26
  import {
27
- UnsupportedFunctionalityError
27
+ InvalidArgumentError,
28
+ InvalidPromptError,
29
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError3
28
30
  } from "@ai-sdk/provider";
29
31
  import {
30
32
  convertBase64ToUint8Array,
31
33
  convertToBase64
32
34
  } from "@ai-sdk/provider-utils";
33
- function convertToMoonshotAIChatMessages(prompt) {
34
- var _a;
35
+
36
+ // src/moonshotai-chat-options.ts
37
+ import { z } from "zod/v4";
38
+ function isMoonshotAIKimiModel(modelId) {
39
+ return getMoonshotAIModelFamily(modelId).startsWith("kimi-");
40
+ }
41
+ function getMoonshotAIModelFamily(modelId) {
42
+ if (modelId === "kimi-k2.5") return "kimi-k2.5";
43
+ if (modelId === "kimi-k2.6") return "kimi-k2.6";
44
+ if (modelId === "kimi-k2.7-code" || modelId === "kimi-k2.7-code-highspeed") {
45
+ return "kimi-k2.7";
46
+ }
47
+ if (modelId === "kimi-k3") return "kimi-k3";
48
+ if (modelId.startsWith("moonshot-v1-")) return "moonshot-v1";
49
+ return "unknown";
50
+ }
51
+ var moonshotaiLanguageModelOptions = z.object({
52
+ /**
53
+ * Whether to use strict JSON schema validation for structured outputs.
54
+ *
55
+ * @default true
56
+ */
57
+ strictJsonSchema: z.boolean().optional(),
58
+ /**
59
+ * Whether to return log probabilities for generated tokens.
60
+ */
61
+ logprobs: z.boolean().optional(),
62
+ /**
63
+ * Number of most likely tokens to return at each token position.
64
+ *
65
+ * Setting this option automatically enables `logprobs`.
66
+ */
67
+ topLogprobs: z.number().int().min(0).max(20).optional(),
68
+ /**
69
+ * Reasoning effort for Kimi K3. Supports `low`, `high`, and `max`;
70
+ * defaults to `max`.
71
+ */
72
+ reasoningEffort: z.enum(["low", "high", "max"]).optional(),
73
+ /**
74
+ * Static predicted content that can accelerate responses when much of the
75
+ * output is known ahead of time.
76
+ */
77
+ prediction: z.object({
78
+ type: z.literal("content"),
79
+ content: z.union([
80
+ z.string(),
81
+ z.array(z.object({ type: z.literal("text"), text: z.string() }))
82
+ ])
83
+ }).optional(),
84
+ /**
85
+ * Thinking configuration for Kimi K2.x models. Kimi K2.5 and K2.6 support
86
+ * enabling or disabling thinking. Kimi K2.7 Code always has thinking
87
+ * enabled.
88
+ */
89
+ thinking: z.object({
90
+ type: z.enum(["enabled", "disabled"]).optional(),
91
+ /**
92
+ * @deprecated Moonshot Chat Completions does not support thinking
93
+ * budgets. Accepted for backwards compatibility, then omitted with a
94
+ * warning.
95
+ */
96
+ budgetTokens: z.number().int().min(1024).optional()
97
+ }).optional(),
98
+ /**
99
+ * Controls preserved reasoning behavior in multi-turn conversations.
100
+ * `disabled` and `interleaved` are compatibility values that leave the
101
+ * request unchanged. `preserved` maps to `thinking.keep: 'all'` for Kimi
102
+ * K2.6. Kimi K2.7 and K3 preserve reasoning by default.
103
+ */
104
+ reasoningHistory: z.enum(["disabled", "interleaved", "preserved"]).optional(),
105
+ /**
106
+ * Used to cache responses for similar requests to optimize cache hit rates.
107
+ * Typically a session or task id.
108
+ */
109
+ promptCacheKey: z.string().optional(),
110
+ /**
111
+ * A stable identifier used to help Moonshot detect users violating usage
112
+ * policies. Recommended to hash the username or email address.
113
+ */
114
+ safetyIdentifier: z.string().optional()
115
+ });
116
+ var moonshotaiMessageProviderOptions = z.object({
117
+ /**
118
+ * The name of the participant represented by the message.
119
+ *
120
+ * Supported on system, user, and assistant messages.
121
+ */
122
+ name: z.string().optional()
123
+ });
124
+ var moonshotaiAssistantMessageProviderOptions = moonshotaiMessageProviderOptions.extend({
125
+ /**
126
+ * Whether the assistant message content is a partial response that Moonshot
127
+ * should continue. Only supported on the final assistant message and cannot
128
+ * be combined with JSON object response format.
129
+ */
130
+ partial: z.literal(true).optional()
131
+ });
132
+ var moonshotaiDynamicToolSchema = z.object({
133
+ type: z.literal("function"),
134
+ name: z.string(),
135
+ description: z.string().optional(),
136
+ inputSchema: z.record(z.string(), z.unknown()),
137
+ strict: z.boolean().optional()
138
+ });
139
+ var moonshotaiAllMessageProviderOptions = moonshotaiAssistantMessageProviderOptions.extend({
140
+ /** Function tools to load at this point in a Kimi K3 conversation. */
141
+ tools: z.array(moonshotaiDynamicToolSchema).optional()
142
+ });
143
+
144
+ // src/moonshotai-prepare-tools.ts
145
+ import {
146
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError2
147
+ } from "@ai-sdk/provider";
148
+
149
+ // src/normalize-json-schema-for-mfjs.ts
150
+ import { UnsupportedFunctionalityError } from "@ai-sdk/provider";
151
+ function isRecord(value) {
152
+ return typeof value === "object" && value !== null && !Array.isArray(value);
153
+ }
154
+ var SCHEMA_ARRAY_KEYS = ["allOf", "anyOf", "oneOf", "prefixItems"];
155
+ var SCHEMA_MAP_KEYS = [
156
+ "properties",
157
+ "patternProperties",
158
+ "$defs",
159
+ "dependentSchemas"
160
+ ];
161
+ var SCHEMA_SINGLE_KEYS = [
162
+ "additionalProperties",
163
+ "propertyNames",
164
+ "items",
165
+ "contains",
166
+ "not",
167
+ "if",
168
+ "then",
169
+ "else"
170
+ ];
171
+ function normalizeJsonSchemaForMFJS(schema) {
172
+ return normalizeDefinition(schema, true);
173
+ }
174
+ function normalizeDefinition(definition, isRoot) {
175
+ if (typeof definition === "boolean" || !isRecord(definition)) {
176
+ if (isRoot) {
177
+ throw new UnsupportedFunctionalityError({
178
+ functionality: 'tool parameters must be a JSON Schema object with type "object" for moonshotai (MFJS)'
179
+ });
180
+ }
181
+ return definition;
182
+ }
183
+ if (isRoot && definition.type !== "object") {
184
+ throw new UnsupportedFunctionalityError({
185
+ functionality: 'tool parameters must be a JSON Schema object with type "object" for moonshotai (MFJS)'
186
+ });
187
+ }
188
+ const result = { ...definition };
189
+ if (Array.isArray(result.items)) {
190
+ const tuple = result.items;
191
+ result.prefixItems = [
192
+ ...Array.isArray(result.prefixItems) ? result.prefixItems : [],
193
+ ...tuple.map((item) => normalizeDefinition(item, false))
194
+ ];
195
+ delete result.items;
196
+ } else if (isRecord(result.items)) {
197
+ result.items = normalizeDefinition(result.items, false);
198
+ }
199
+ if (typeof result.type === "string" && Array.isArray(result.anyOf)) {
200
+ const parentType = result.type;
201
+ delete result.type;
202
+ result.anyOf = result.anyOf.map(
203
+ (branch) => isRecord(branch) && branch.type == null ? { type: parentType, ...branch } : branch
204
+ );
205
+ }
206
+ for (const key of SCHEMA_ARRAY_KEYS) {
207
+ const value = result[key];
208
+ if (Array.isArray(value)) {
209
+ result[key] = value.map((item) => normalizeDefinition(item, false));
210
+ }
211
+ }
212
+ for (const key of SCHEMA_MAP_KEYS) {
213
+ const value = result[key];
214
+ if (isRecord(value)) {
215
+ result[key] = Object.fromEntries(
216
+ Object.entries(value).map(([k, v]) => [
217
+ k,
218
+ normalizeDefinition(v, false)
219
+ ])
220
+ );
221
+ }
222
+ }
223
+ for (const key of SCHEMA_SINGLE_KEYS) {
224
+ const value = result[key];
225
+ if (isRecord(value) || typeof value === "boolean") {
226
+ result[key] = normalizeDefinition(value, false);
227
+ }
228
+ }
229
+ return result;
230
+ }
231
+
232
+ // src/moonshotai-prepare-tools.ts
233
+ function prepareTools({
234
+ tools,
235
+ toolChoice,
236
+ modelId
237
+ }) {
238
+ tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
239
+ const toolWarnings = [];
240
+ if (tools == null) {
241
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
242
+ }
243
+ const moonshotTools = [];
244
+ for (const tool of tools) {
245
+ if (tool.type === "provider") {
246
+ toolWarnings.push({
247
+ type: "unsupported",
248
+ feature: `provider-defined tool ${tool.id}`
249
+ });
250
+ } else {
251
+ moonshotTools.push({
252
+ type: "function",
253
+ function: {
254
+ name: tool.name,
255
+ description: tool.description,
256
+ parameters: normalizeJsonSchemaForMFJS(tool.inputSchema),
257
+ ...tool.strict != null ? { strict: tool.strict } : {}
258
+ }
259
+ });
260
+ }
261
+ }
262
+ if (toolChoice == null) {
263
+ return { tools: moonshotTools, toolChoice: void 0, toolWarnings };
264
+ }
265
+ const type = toolChoice.type;
266
+ switch (type) {
267
+ case "auto":
268
+ case "none":
269
+ return { tools: moonshotTools, toolChoice: type, toolWarnings };
270
+ case "required":
271
+ if (modelId === "kimi-k2.6" || modelId === "kimi-k2.7-code" || modelId === "kimi-k2.7-code-highspeed") {
272
+ toolWarnings.push({
273
+ type: "unsupported",
274
+ feature: `tool choice "required" for model "${modelId}"`,
275
+ details: 'Moonshot AI rejects required tool choice for this model. The setting has been omitted; use "auto" or select a specific tool instead.'
276
+ });
277
+ return {
278
+ tools: moonshotTools,
279
+ toolChoice: void 0,
280
+ toolWarnings
281
+ };
282
+ }
283
+ return { tools: moonshotTools, toolChoice: type, toolWarnings };
284
+ case "tool":
285
+ return {
286
+ tools: moonshotTools,
287
+ toolChoice: {
288
+ type: "function",
289
+ function: { name: toolChoice.toolName }
290
+ },
291
+ toolWarnings
292
+ };
293
+ default: {
294
+ const _exhaustiveCheck = type;
295
+ throw new UnsupportedFunctionalityError2({
296
+ functionality: `tool choice type: ${_exhaustiveCheck}`
297
+ });
298
+ }
299
+ }
300
+ }
301
+
302
+ // src/convert-to-moonshotai-chat-messages.ts
303
+ function parseMoonshotAIMessageProviderOptions({
304
+ providerOptions,
305
+ providerOptionsName
306
+ }) {
307
+ const value = providerOptions == null ? void 0 : providerOptions[providerOptionsName];
308
+ if (value == null) {
309
+ return void 0;
310
+ }
311
+ const result = moonshotaiAllMessageProviderOptions.safeParse(value);
312
+ if (!result.success) {
313
+ throw new InvalidArgumentError({
314
+ argument: "providerOptions",
315
+ message: `invalid ${providerOptionsName} provider options`,
316
+ cause: result.error
317
+ });
318
+ }
319
+ return result.data;
320
+ }
321
+ var supportedImageMediaTypes = [
322
+ "image/jpeg",
323
+ "image/png",
324
+ "image/gif",
325
+ "image/webp",
326
+ "image/bmp",
327
+ "image/heic",
328
+ "image/heif"
329
+ ];
330
+ var supportedVideoMediaTypes = [
331
+ "video/mp4",
332
+ "video/mpeg",
333
+ "video/mov",
334
+ "video/avi",
335
+ "video/x-flv",
336
+ "video/mpg",
337
+ "video/webm",
338
+ "video/wmv",
339
+ "video/3gpp"
340
+ ];
341
+ function validateMediaType({
342
+ mediaType,
343
+ supportedMediaTypes,
344
+ topLevelMediaType
345
+ }) {
346
+ const normalizedMediaType = mediaType === `${topLevelMediaType}/*` ? topLevelMediaType === "image" ? "image/jpeg" : "video/mp4" : mediaType;
347
+ if (!supportedMediaTypes.includes(normalizedMediaType)) {
348
+ throw new UnsupportedFunctionalityError3({
349
+ functionality: `file part media type ${normalizedMediaType}`
350
+ });
351
+ }
352
+ return normalizedMediaType;
353
+ }
354
+ function convertToMoonshotAIChatMessages({
355
+ modelId,
356
+ prompt,
357
+ providerOptionsName = "moonshotai",
358
+ responseFormat
359
+ }) {
360
+ var _a, _b;
35
361
  const messages = [];
36
- for (const { role, content } of prompt) {
362
+ const warnings = [];
363
+ const modelFamily = modelId == null ? "unknown" : getMoonshotAIModelFamily(modelId);
364
+ for (const [index, { role, content, providerOptions }] of prompt.entries()) {
365
+ const moonshotMessageOptions = parseMoonshotAIMessageProviderOptions({
366
+ providerOptions,
367
+ providerOptionsName
368
+ });
369
+ if ((moonshotMessageOptions == null ? void 0 : moonshotMessageOptions.partial) === true && role !== "assistant") {
370
+ throw new InvalidPromptError({
371
+ prompt,
372
+ message: "Moonshot AI Partial Mode requires `partial: true` on an assistant message."
373
+ });
374
+ }
375
+ if ((moonshotMessageOptions == null ? void 0 : moonshotMessageOptions.tools) != null && role !== "system") {
376
+ throw new InvalidPromptError({
377
+ prompt,
378
+ message: "Moonshot dynamic tools must be configured on a system message."
379
+ });
380
+ }
37
381
  switch (role) {
38
382
  case "system": {
39
- messages.push({ role: "system", content });
383
+ if ((_a = moonshotMessageOptions == null ? void 0 : moonshotMessageOptions.tools) == null ? void 0 : _a.length) {
384
+ if (content.length > 0) {
385
+ throw new InvalidPromptError({
386
+ prompt,
387
+ message: "A Moonshot dynamic-tool system message must use empty content because the API forbids content alongside tools."
388
+ });
389
+ }
390
+ if (modelFamily !== "kimi-k3" && modelFamily !== "unknown") {
391
+ warnings.push({
392
+ type: "unsupported",
393
+ feature: `dynamic tool loading for model "${modelId}"`,
394
+ details: "Moonshot documents dynamic tool loading only for Kimi K3. The dynamic system message has been omitted."
395
+ });
396
+ break;
397
+ }
398
+ const { tools, toolWarnings } = prepareTools({
399
+ modelId: modelId != null ? modelId : "custom-model",
400
+ tools: moonshotMessageOptions.tools
401
+ });
402
+ warnings.push(...toolWarnings);
403
+ messages.push({ role: "system", tools: tools != null ? tools : [] });
404
+ break;
405
+ }
406
+ messages.push({
407
+ role: "system",
408
+ content,
409
+ ...(moonshotMessageOptions == null ? void 0 : moonshotMessageOptions.name) != null && {
410
+ name: moonshotMessageOptions.name
411
+ }
412
+ });
40
413
  break;
41
414
  }
42
415
  case "user": {
43
416
  if (content.length === 1 && content[0].type === "text") {
44
417
  messages.push({
45
418
  role: "user",
46
- content: content[0].text
419
+ content: content[0].text,
420
+ ...(moonshotMessageOptions == null ? void 0 : moonshotMessageOptions.name) != null && {
421
+ name: moonshotMessageOptions.name
422
+ }
47
423
  });
48
424
  break;
49
425
  }
@@ -56,7 +432,11 @@ function convertToMoonshotAIChatMessages(prompt) {
56
432
  }
57
433
  case "file": {
58
434
  if (part.mediaType.startsWith("image/")) {
59
- const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
435
+ const mediaType = validateMediaType({
436
+ mediaType: part.mediaType,
437
+ supportedMediaTypes: supportedImageMediaTypes,
438
+ topLevelMediaType: "image"
439
+ });
60
440
  return {
61
441
  type: "image_url",
62
442
  image_url: {
@@ -65,7 +445,11 @@ function convertToMoonshotAIChatMessages(prompt) {
65
445
  };
66
446
  }
67
447
  if (part.mediaType.startsWith("video/")) {
68
- const mediaType = part.mediaType === "video/*" ? "video/mp4" : part.mediaType;
448
+ const mediaType = validateMediaType({
449
+ mediaType: part.mediaType,
450
+ supportedMediaTypes: supportedVideoMediaTypes,
451
+ topLevelMediaType: "video"
452
+ });
69
453
  return {
70
454
  type: "video_url",
71
455
  video_url: {
@@ -82,16 +466,33 @@ function convertToMoonshotAIChatMessages(prompt) {
82
466
  text: textContent
83
467
  };
84
468
  }
85
- throw new UnsupportedFunctionalityError({
469
+ throw new UnsupportedFunctionalityError3({
86
470
  functionality: `file part media type ${part.mediaType}`
87
471
  });
88
472
  }
89
473
  }
90
- })
474
+ }),
475
+ ...(moonshotMessageOptions == null ? void 0 : moonshotMessageOptions.name) != null && {
476
+ name: moonshotMessageOptions.name
477
+ }
91
478
  });
92
479
  break;
93
480
  }
94
481
  case "assistant": {
482
+ if ((moonshotMessageOptions == null ? void 0 : moonshotMessageOptions.partial) === true) {
483
+ if (index !== prompt.length - 1) {
484
+ throw new InvalidPromptError({
485
+ prompt,
486
+ message: "Moonshot AI Partial Mode requires the partial assistant message to be the final message."
487
+ });
488
+ }
489
+ if ((responseFormat == null ? void 0 : responseFormat.type) === "json_object") {
490
+ throw new InvalidPromptError({
491
+ prompt,
492
+ message: "Moonshot AI Partial Mode cannot be combined with JSON object response format."
493
+ });
494
+ }
495
+ }
95
496
  let text = "";
96
497
  let reasoning = "";
97
498
  const toolCalls = [];
@@ -121,12 +522,24 @@ function convertToMoonshotAIChatMessages(prompt) {
121
522
  messages.push({
122
523
  role: "assistant",
123
524
  content: toolCalls.length > 0 ? text || null : text,
525
+ ...(moonshotMessageOptions == null ? void 0 : moonshotMessageOptions.name) != null && {
526
+ name: moonshotMessageOptions.name
527
+ },
528
+ ...(moonshotMessageOptions == null ? void 0 : moonshotMessageOptions.partial) === true && {
529
+ partial: true
530
+ },
124
531
  ...reasoning.length > 0 ? { reasoning_content: reasoning } : {},
125
532
  tool_calls: toolCalls.length > 0 ? toolCalls : void 0
126
533
  });
127
534
  break;
128
535
  }
129
536
  case "tool": {
537
+ if ((moonshotMessageOptions == null ? void 0 : moonshotMessageOptions.name) != null) {
538
+ warnings.push({
539
+ type: "unsupported",
540
+ feature: "message name on tool messages"
541
+ });
542
+ }
130
543
  for (const toolResponse of content) {
131
544
  if (toolResponse.type === "tool-approval-response") {
132
545
  continue;
@@ -139,7 +552,7 @@ function convertToMoonshotAIChatMessages(prompt) {
139
552
  contentValue = output.value;
140
553
  break;
141
554
  case "execution-denied":
142
- contentValue = (_a = output.reason) != null ? _a : "Tool call execution denied.";
555
+ contentValue = (_b = output.reason) != null ? _b : "Tool call execution denied.";
143
556
  break;
144
557
  case "content":
145
558
  case "json":
@@ -161,7 +574,7 @@ function convertToMoonshotAIChatMessages(prompt) {
161
574
  }
162
575
  }
163
576
  }
164
- return messages;
577
+ return { messages, warnings };
165
578
  }
166
579
 
167
580
  // src/convert-moonshotai-chat-usage.ts
@@ -235,75 +648,100 @@ function mapMoonshotAIFinishReason(finishReason) {
235
648
 
236
649
  // src/moonshotai-chat-api-types.ts
237
650
  import { lazySchema, zodSchema } from "@ai-sdk/provider-utils";
238
- import { z } from "zod/v4";
239
- var tokenUsageSchema = z.object({
240
- prompt_tokens: z.number().nullish(),
241
- completion_tokens: z.number().nullish(),
242
- cached_tokens: z.number().nullish(),
243
- total_tokens: z.number().nullish(),
244
- prompt_tokens_details: z.object({
245
- cached_tokens: z.number().nullish()
651
+ import { z as z2 } from "zod/v4";
652
+ var tokenUsageSchema = z2.looseObject({
653
+ prompt_tokens: z2.number().nullish(),
654
+ completion_tokens: z2.number().nullish(),
655
+ cached_tokens: z2.number().nullish(),
656
+ total_tokens: z2.number().nullish(),
657
+ prompt_tokens_details: z2.looseObject({
658
+ cached_tokens: z2.number().nullish()
246
659
  }).nullish(),
247
- completion_tokens_details: z.object({
248
- reasoning_tokens: z.number().nullish()
660
+ completion_tokens_details: z2.looseObject({
661
+ reasoning_tokens: z2.number().nullish()
249
662
  }).nullish()
250
663
  }).nullish();
251
- var moonshotAIErrorSchema = z.object({
252
- error: z.object({
253
- message: z.string(),
254
- type: z.string().nullish()
664
+ var moonshotAIErrorSchema = z2.object({
665
+ error: z2.object({
666
+ message: z2.string(),
667
+ type: z2.string().nullish(),
668
+ code: z2.string().nullish()
255
669
  })
256
670
  });
257
- var moonshotAIChatResponseSchema = z.object({
258
- id: z.string().nullish(),
259
- created: z.number().nullish(),
260
- model: z.string().nullish(),
261
- choices: z.array(
262
- z.object({
263
- message: z.object({
264
- role: z.literal("assistant").nullish(),
265
- content: z.string().nullish(),
266
- reasoning_content: z.string().nullish(),
267
- tool_calls: z.array(
268
- z.object({
269
- id: z.string().nullish(),
270
- function: z.object({
271
- name: z.string(),
272
- arguments: z.string()
671
+ var moonshotAIChatLogprobSchema = z2.object({
672
+ token: z2.string(),
673
+ logprob: z2.number(),
674
+ bytes: z2.array(z2.number()).nullable(),
675
+ top_logprobs: z2.array(
676
+ z2.object({
677
+ token: z2.string(),
678
+ logprob: z2.number(),
679
+ bytes: z2.array(z2.number()).nullable()
680
+ })
681
+ )
682
+ });
683
+ var moonshotAIChatLogprobsSchema = z2.object({
684
+ content: z2.array(moonshotAIChatLogprobSchema).nullish()
685
+ }).nullish();
686
+ var moonshotAIChatResponseSchema = z2.object({
687
+ id: z2.string().nullish(),
688
+ created: z2.number().nullish(),
689
+ model: z2.string().nullish(),
690
+ object: z2.literal("chat.completion").nullish(),
691
+ choices: z2.array(
692
+ z2.object({
693
+ index: z2.number().nullish(),
694
+ message: z2.object({
695
+ role: z2.literal("assistant").nullish(),
696
+ content: z2.string().nullish(),
697
+ reasoning_content: z2.string().nullish(),
698
+ tool_calls: z2.array(
699
+ z2.object({
700
+ id: z2.string().nullish(),
701
+ type: z2.literal("function").nullish(),
702
+ function: z2.object({
703
+ name: z2.string(),
704
+ arguments: z2.string()
273
705
  })
274
706
  })
275
707
  ).nullish()
276
708
  }),
277
- finish_reason: z.string().nullish()
709
+ logprobs: moonshotAIChatLogprobsSchema,
710
+ finish_reason: z2.string().nullish()
278
711
  })
279
712
  ),
280
713
  usage: tokenUsageSchema
281
714
  });
282
715
  var moonshotAIChatChunkSchema = lazySchema(
283
716
  () => zodSchema(
284
- z.union([
285
- z.object({
286
- id: z.string().nullish(),
287
- created: z.number().nullish(),
288
- model: z.string().nullish(),
289
- choices: z.array(
290
- z.object({
291
- delta: z.object({
292
- role: z.literal("assistant").nullish(),
293
- content: z.string().nullish(),
294
- reasoning_content: z.string().nullish(),
295
- tool_calls: z.array(
296
- z.object({
297
- index: z.number(),
298
- id: z.string().nullish(),
299
- function: z.object({
300
- name: z.string().nullish(),
301
- arguments: z.string().nullish()
717
+ z2.union([
718
+ z2.object({
719
+ id: z2.string().nullish(),
720
+ created: z2.number().nullish(),
721
+ model: z2.string().nullish(),
722
+ object: z2.literal("chat.completion.chunk").nullish(),
723
+ choices: z2.array(
724
+ z2.object({
725
+ index: z2.number().nullish(),
726
+ delta: z2.object({
727
+ role: z2.literal("assistant").nullish(),
728
+ content: z2.string().nullish(),
729
+ reasoning_content: z2.string().nullish(),
730
+ tool_calls: z2.array(
731
+ z2.object({
732
+ index: z2.number().nullish(),
733
+ id: z2.string().nullish(),
734
+ type: z2.literal("function").nullish(),
735
+ function: z2.object({
736
+ name: z2.string().nullish(),
737
+ arguments: z2.string().nullish()
302
738
  })
303
739
  })
304
740
  ).nullish()
305
741
  }).nullish(),
306
- finish_reason: z.string().nullish()
742
+ logprobs: moonshotAIChatLogprobsSchema,
743
+ finish_reason: z2.string().nullish(),
744
+ usage: tokenUsageSchema
307
745
  })
308
746
  ),
309
747
  usage: tokenUsageSchema
@@ -313,177 +751,6 @@ var moonshotAIChatChunkSchema = lazySchema(
313
751
  )
314
752
  );
315
753
 
316
- // src/moonshotai-chat-options.ts
317
- import { z as z2 } from "zod/v4";
318
- var moonshotaiLanguageModelOptions = z2.object({
319
- /**
320
- * Reasoning effort for Kimi K3.
321
- */
322
- reasoningEffort: z2.enum(["low", "high", "max"]).optional(),
323
- thinking: z2.object({
324
- type: z2.enum(["enabled", "disabled"]).optional(),
325
- budgetTokens: z2.number().int().min(1024).optional()
326
- }).optional(),
327
- reasoningHistory: z2.enum(["disabled", "interleaved", "preserved"]).optional(),
328
- /**
329
- * Used to cache responses for similar requests to optimize cache hit rates.
330
- * Typically a session or task id.
331
- */
332
- promptCacheKey: z2.string().optional(),
333
- /**
334
- * A stable identifier used to help Moonshot detect users violating usage
335
- * policies. Recommended to hash the username or email address.
336
- */
337
- safetyIdentifier: z2.string().optional()
338
- });
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
-
348
- // src/normalize-json-schema-for-mfjs.ts
349
- import { UnsupportedFunctionalityError as UnsupportedFunctionalityError2 } from "@ai-sdk/provider";
350
- function isRecord(value) {
351
- return typeof value === "object" && value !== null && !Array.isArray(value);
352
- }
353
- var SCHEMA_ARRAY_KEYS = ["allOf", "anyOf", "oneOf", "prefixItems"];
354
- var SCHEMA_MAP_KEYS = [
355
- "properties",
356
- "patternProperties",
357
- "$defs",
358
- "dependentSchemas"
359
- ];
360
- var SCHEMA_SINGLE_KEYS = [
361
- "additionalProperties",
362
- "propertyNames",
363
- "items",
364
- "contains",
365
- "not",
366
- "if",
367
- "then",
368
- "else"
369
- ];
370
- function normalizeJsonSchemaForMFJS(schema) {
371
- return normalizeDefinition(schema, true);
372
- }
373
- function normalizeDefinition(definition, isRoot) {
374
- if (typeof definition === "boolean" || !isRecord(definition)) {
375
- if (isRoot) {
376
- throw new UnsupportedFunctionalityError2({
377
- functionality: 'tool parameters must be a JSON Schema object with type "object" for moonshotai (MFJS)'
378
- });
379
- }
380
- return definition;
381
- }
382
- if (isRoot && definition.type !== "object") {
383
- throw new UnsupportedFunctionalityError2({
384
- functionality: 'tool parameters must be a JSON Schema object with type "object" for moonshotai (MFJS)'
385
- });
386
- }
387
- const result = { ...definition };
388
- if (Array.isArray(result.items)) {
389
- const tuple = result.items;
390
- result.prefixItems = [
391
- ...Array.isArray(result.prefixItems) ? result.prefixItems : [],
392
- ...tuple.map((item) => normalizeDefinition(item, false))
393
- ];
394
- delete result.items;
395
- } else if (isRecord(result.items)) {
396
- result.items = normalizeDefinition(result.items, false);
397
- }
398
- if (typeof result.type === "string" && Array.isArray(result.anyOf)) {
399
- const parentType = result.type;
400
- delete result.type;
401
- result.anyOf = result.anyOf.map(
402
- (branch) => isRecord(branch) && branch.type == null ? { type: parentType, ...branch } : branch
403
- );
404
- }
405
- for (const key of SCHEMA_ARRAY_KEYS) {
406
- const value = result[key];
407
- if (Array.isArray(value)) {
408
- result[key] = value.map((item) => normalizeDefinition(item, false));
409
- }
410
- }
411
- for (const key of SCHEMA_MAP_KEYS) {
412
- const value = result[key];
413
- if (isRecord(value)) {
414
- result[key] = Object.fromEntries(
415
- Object.entries(value).map(([k, v]) => [
416
- k,
417
- normalizeDefinition(v, false)
418
- ])
419
- );
420
- }
421
- }
422
- for (const key of SCHEMA_SINGLE_KEYS) {
423
- const value = result[key];
424
- if (isRecord(value) || typeof value === "boolean") {
425
- result[key] = normalizeDefinition(value, false);
426
- }
427
- }
428
- return result;
429
- }
430
-
431
- // src/moonshotai-prepare-tools.ts
432
- function prepareTools({
433
- tools,
434
- toolChoice
435
- }) {
436
- tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
437
- const toolWarnings = [];
438
- if (tools == null) {
439
- return { tools: void 0, toolChoice: void 0, toolWarnings };
440
- }
441
- const moonshotTools = [];
442
- for (const tool of tools) {
443
- if (tool.type === "provider") {
444
- toolWarnings.push({
445
- type: "unsupported",
446
- feature: `provider-defined tool ${tool.id}`
447
- });
448
- } else {
449
- moonshotTools.push({
450
- type: "function",
451
- function: {
452
- name: tool.name,
453
- description: tool.description,
454
- parameters: normalizeJsonSchemaForMFJS(tool.inputSchema),
455
- ...tool.strict != null ? { strict: tool.strict } : {}
456
- }
457
- });
458
- }
459
- }
460
- if (toolChoice == null) {
461
- return { tools: moonshotTools, toolChoice: void 0, toolWarnings };
462
- }
463
- const type = toolChoice.type;
464
- switch (type) {
465
- case "auto":
466
- case "none":
467
- case "required":
468
- return { tools: moonshotTools, toolChoice: type, toolWarnings };
469
- case "tool":
470
- return {
471
- tools: moonshotTools,
472
- toolChoice: {
473
- type: "function",
474
- function: { name: toolChoice.toolName }
475
- },
476
- toolWarnings
477
- };
478
- default: {
479
- const _exhaustiveCheck = type;
480
- throw new UnsupportedFunctionalityError3({
481
- functionality: `tool choice type: ${_exhaustiveCheck}`
482
- });
483
- }
484
- }
485
- }
486
-
487
754
  // src/moonshotai-chat-language-model.ts
488
755
  var MoonshotAIChatLanguageModel = class {
489
756
  constructor(modelId, config) {
@@ -523,13 +790,12 @@ var MoonshotAIChatLanguageModel = class {
523
790
  toolChoice,
524
791
  tools
525
792
  }) {
526
- var _a, _b;
793
+ var _a, _b, _c;
527
794
  const moonshotOptions = (_a = await parseProviderOptions({
528
795
  provider: this.providerOptionsName,
529
796
  providerOptions,
530
797
  schema: moonshotaiLanguageModelOptions
531
798
  })) != null ? _a : {};
532
- const messages = convertToMoonshotAIChatMessages(prompt);
533
799
  const allWarnings = [];
534
800
  if (topK != null) {
535
801
  allWarnings.push({ type: "unsupported", feature: "topK" });
@@ -537,22 +803,141 @@ var MoonshotAIChatLanguageModel = class {
537
803
  if (seed != null) {
538
804
  allWarnings.push({ type: "unsupported", feature: "seed" });
539
805
  }
806
+ const supportsSamplingOptions = !isMoonshotAIKimiModel(this.modelId);
807
+ if (!supportsSamplingOptions && temperature != null) {
808
+ allWarnings.push({
809
+ type: "unsupported",
810
+ feature: "temperature",
811
+ details: `temperature is fixed by model "${this.modelId}" and has been omitted.`
812
+ });
813
+ }
814
+ if (!supportsSamplingOptions && topP != null) {
815
+ allWarnings.push({
816
+ type: "unsupported",
817
+ feature: "topP",
818
+ details: `topP is fixed by model "${this.modelId}" and has been omitted.`
819
+ });
820
+ }
821
+ if (!supportsSamplingOptions && frequencyPenalty != null) {
822
+ allWarnings.push({
823
+ type: "unsupported",
824
+ feature: "frequencyPenalty",
825
+ details: `frequencyPenalty is fixed by model "${this.modelId}" and has been omitted.`
826
+ });
827
+ }
828
+ if (!supportsSamplingOptions && presencePenalty != null) {
829
+ allWarnings.push({
830
+ type: "unsupported",
831
+ feature: "presencePenalty",
832
+ details: `presencePenalty is fixed by model "${this.modelId}" and has been omitted.`
833
+ });
834
+ }
540
835
  const {
541
836
  tools: moonshotTools,
542
837
  toolChoice: moonshotToolChoice,
543
838
  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 {
839
+ } = prepareTools({ tools, toolChoice, modelId: this.modelId });
840
+ const modelFamily = getMoonshotAIModelFamily(this.modelId);
841
+ const requestedThinking = moonshotOptions.thinking;
842
+ const requestedReasoningEffort = moonshotOptions.reasoningEffort;
843
+ const preserveReasoning = moonshotOptions.reasoningHistory === "preserved";
844
+ if ((requestedThinking == null ? void 0 : requestedThinking.budgetTokens) != null) {
845
+ allWarnings.push({
846
+ type: "other",
847
+ message: "providerOptions.moonshotai.thinking.budgetTokens is deprecated because Moonshot Chat Completions does not support budget_tokens. The option has been omitted."
848
+ });
849
+ }
850
+ let thinking;
851
+ let reasoningEffort;
852
+ const warnUnsupportedReasoningEffort = () => {
853
+ if (requestedReasoningEffort != null) {
551
854
  allWarnings.push({
552
855
  type: "unsupported",
553
- feature: `reasoningHistory 'preserved' is not supported by model "${this.modelId}"`
856
+ feature: "reasoningEffort",
857
+ details: `reasoningEffort is only supported by Kimi K3 and has been omitted for model "${this.modelId}".`
554
858
  });
555
859
  }
860
+ };
861
+ switch (modelFamily) {
862
+ case "kimi-k3": {
863
+ if (requestedThinking != null) {
864
+ allWarnings.push({
865
+ type: "unsupported",
866
+ feature: "thinking",
867
+ details: "Kimi K3 always reasons and does not accept the thinking field. The option has been omitted."
868
+ });
869
+ }
870
+ reasoningEffort = requestedReasoningEffort;
871
+ break;
872
+ }
873
+ case "kimi-k2.7": {
874
+ warnUnsupportedReasoningEffort();
875
+ if ((requestedThinking == null ? void 0 : requestedThinking.type) === "disabled") {
876
+ allWarnings.push({
877
+ type: "unsupported",
878
+ feature: 'thinking.type "disabled"',
879
+ details: "Kimi K2.7 thinking cannot be disabled."
880
+ });
881
+ } else if ((requestedThinking == null ? void 0 : requestedThinking.type) === "enabled") {
882
+ thinking = { type: "enabled" };
883
+ }
884
+ break;
885
+ }
886
+ case "kimi-k2.6": {
887
+ warnUnsupportedReasoningEffort();
888
+ const thinkingType = requestedThinking == null ? void 0 : requestedThinking.type;
889
+ if (thinkingType != null || preserveReasoning) {
890
+ thinking = {
891
+ type: thinkingType != null ? thinkingType : "enabled",
892
+ ...preserveReasoning ? { keep: "all" } : {}
893
+ };
894
+ }
895
+ break;
896
+ }
897
+ case "kimi-k2.5": {
898
+ warnUnsupportedReasoningEffort();
899
+ const thinkingType = requestedThinking == null ? void 0 : requestedThinking.type;
900
+ if (thinkingType != null) {
901
+ thinking = { type: thinkingType };
902
+ }
903
+ if (preserveReasoning) {
904
+ allWarnings.push({
905
+ type: "unsupported",
906
+ feature: `reasoningHistory 'preserved' is not supported by model "${this.modelId}"`
907
+ });
908
+ }
909
+ break;
910
+ }
911
+ case "moonshot-v1": {
912
+ warnUnsupportedReasoningEffort();
913
+ if (requestedThinking != null) {
914
+ allWarnings.push({
915
+ type: "unsupported",
916
+ feature: "thinking",
917
+ details: `thinking is not supported by model "${this.modelId}" and has been omitted.`
918
+ });
919
+ }
920
+ if (preserveReasoning) {
921
+ allWarnings.push({
922
+ type: "unsupported",
923
+ feature: `reasoningHistory 'preserved' is not supported by model "${this.modelId}"`
924
+ });
925
+ }
926
+ break;
927
+ }
928
+ case "unknown": {
929
+ reasoningEffort = requestedReasoningEffort;
930
+ if ((requestedThinking == null ? void 0 : requestedThinking.type) != null) {
931
+ thinking = { type: requestedThinking.type };
932
+ }
933
+ if (preserveReasoning) {
934
+ allWarnings.push({
935
+ type: "unsupported",
936
+ feature: `reasoningHistory 'preserved' is not supported by model "${this.modelId}"`
937
+ });
938
+ }
939
+ break;
940
+ }
556
941
  }
557
942
  let response_format;
558
943
  if ((responseFormat == null ? void 0 : responseFormat.type) === "json") {
@@ -562,40 +947,44 @@ var MoonshotAIChatLanguageModel = class {
562
947
  type: "json_schema",
563
948
  json_schema: {
564
949
  name: (_b = responseFormat.name) != null ? _b : "response",
565
- schema: schemaWithoutDollarSchema,
566
- ...responseFormat.description != null && {
567
- description: responseFormat.description
568
- }
950
+ strict: (_c = moonshotOptions.strictJsonSchema) != null ? _c : true,
951
+ schema: normalizeJsonSchemaForMFJS(schemaWithoutDollarSchema)
569
952
  }
570
953
  };
571
954
  } else {
572
955
  response_format = { type: "json_object" };
573
956
  }
574
957
  }
958
+ const { messages, warnings: messageWarnings } = convertToMoonshotAIChatMessages({
959
+ modelId: this.modelId,
960
+ prompt,
961
+ providerOptionsName: this.providerOptionsName,
962
+ responseFormat: response_format
963
+ });
964
+ allWarnings.push(...messageWarnings);
575
965
  return {
576
966
  args: {
577
967
  model: this.modelId,
578
- max_tokens: maxOutputTokens,
579
- temperature,
580
- top_p: topP,
581
- frequency_penalty: frequencyPenalty,
582
- presence_penalty: presencePenalty,
968
+ ...(moonshotOptions.logprobs === true || moonshotOptions.topLogprobs != null) && { logprobs: true },
969
+ ...moonshotOptions.topLogprobs != null && {
970
+ top_logprobs: moonshotOptions.topLogprobs
971
+ },
972
+ max_completion_tokens: maxOutputTokens,
973
+ temperature: supportsSamplingOptions ? temperature : void 0,
974
+ top_p: supportsSamplingOptions ? topP : void 0,
975
+ frequency_penalty: supportsSamplingOptions ? frequencyPenalty : void 0,
976
+ presence_penalty: supportsSamplingOptions ? presencePenalty : void 0,
583
977
  response_format,
584
978
  stop: stopSequences,
585
979
  messages,
586
980
  tools: moonshotTools,
587
981
  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
982
+ ...moonshotOptions.prediction != null && {
983
+ prediction: moonshotOptions.prediction
984
+ },
985
+ ...thinking != null ? { thinking } : {},
986
+ ...reasoningEffort != null && {
987
+ reasoning_effort: reasoningEffort
599
988
  },
600
989
  ...moonshotOptions.promptCacheKey != null && {
601
990
  prompt_cache_key: moonshotOptions.promptCacheKey
@@ -655,6 +1044,21 @@ var MoonshotAIChatLanguageModel = class {
655
1044
  raw: (_e = choice.finish_reason) != null ? _e : void 0
656
1045
  },
657
1046
  usage: convertMoonshotAIChatUsage(responseBody.usage),
1047
+ providerMetadata: {
1048
+ [this.providerOptionsName]: {
1049
+ ...responseBody.object != null && {
1050
+ responseObject: responseBody.object
1051
+ },
1052
+ ...choice.index != null && { choiceIndex: choice.index },
1053
+ ...choice.message.role != null && {
1054
+ messageRole: choice.message.role
1055
+ },
1056
+ ...choice.message.tool_calls != null && {
1057
+ toolCallTypes: choice.message.tool_calls.map((toolCall) => toolCall.type).filter((type) => type != null)
1058
+ },
1059
+ ...choice.logprobs != null && { logprobs: choice.logprobs }
1060
+ }
1061
+ },
658
1062
  request: { body: args },
659
1063
  response: {
660
1064
  ...getResponseMetadata(responseBody),
@@ -693,10 +1097,17 @@ var MoonshotAIChatLanguageModel = class {
693
1097
  unified: "other",
694
1098
  raw: void 0
695
1099
  };
696
- let usage = void 0;
1100
+ let topLevelUsage = void 0;
1101
+ let choiceUsage = void 0;
1102
+ const contentLogprobs = [];
1103
+ const providerOptionsName = this.providerOptionsName;
697
1104
  let isFirstChunk = true;
698
1105
  let isActiveReasoning = false;
699
1106
  let isActiveText = false;
1107
+ let responseObject;
1108
+ let choiceIndex;
1109
+ let messageRole;
1110
+ const toolCallTypes = /* @__PURE__ */ new Map();
700
1111
  return {
701
1112
  stream: response.pipeThrough(
702
1113
  new TransformStream({
@@ -704,7 +1115,7 @@ var MoonshotAIChatLanguageModel = class {
704
1115
  controller.enqueue({ type: "stream-start", warnings });
705
1116
  },
706
1117
  transform(chunk, controller) {
707
- var _a2, _b2, _c, _d, _e, _f;
1118
+ var _a2, _b2, _c, _d, _e, _f, _g, _h;
708
1119
  if (options.includeRawChunks) {
709
1120
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
710
1121
  }
@@ -716,7 +1127,7 @@ var MoonshotAIChatLanguageModel = class {
716
1127
  const value = chunk.value;
717
1128
  if ("error" in value) {
718
1129
  finishReason = { unified: "error", raw: void 0 };
719
- controller.enqueue({ type: "error", error: value.error.message });
1130
+ controller.enqueue({ type: "error", error: value.error });
720
1131
  return;
721
1132
  }
722
1133
  if (isFirstChunk) {
@@ -727,19 +1138,34 @@ var MoonshotAIChatLanguageModel = class {
727
1138
  });
728
1139
  }
729
1140
  if (value.usage != null) {
730
- usage = value.usage;
1141
+ topLevelUsage = value.usage;
1142
+ }
1143
+ if (value.object != null) {
1144
+ responseObject = value.object;
731
1145
  }
732
1146
  const choice = value.choices[0];
1147
+ if ((choice == null ? void 0 : choice.usage) != null) {
1148
+ choiceUsage = choice.usage;
1149
+ }
1150
+ if ((choice == null ? void 0 : choice.index) != null) {
1151
+ choiceIndex = choice.index;
1152
+ }
733
1153
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
734
1154
  finishReason = {
735
1155
  unified: mapMoonshotAIFinishReason(choice.finish_reason),
736
1156
  raw: choice.finish_reason
737
1157
  };
738
1158
  }
1159
+ if (((_a2 = choice == null ? void 0 : choice.logprobs) == null ? void 0 : _a2.content) != null) {
1160
+ contentLogprobs.push(...choice.logprobs.content);
1161
+ }
739
1162
  if ((choice == null ? void 0 : choice.delta) == null) {
740
1163
  return;
741
1164
  }
742
1165
  const delta = choice.delta;
1166
+ if (delta.role != null) {
1167
+ messageRole = delta.role;
1168
+ }
743
1169
  const reasoningContent = delta.reasoning_content;
744
1170
  if (reasoningContent) {
745
1171
  if (!isActiveReasoning) {
@@ -781,8 +1207,14 @@ var MoonshotAIChatLanguageModel = class {
781
1207
  });
782
1208
  isActiveReasoning = false;
783
1209
  }
784
- for (const toolCallDelta of delta.tool_calls) {
785
- const index = toolCallDelta.index;
1210
+ for (const [
1211
+ fallbackIndex,
1212
+ toolCallDelta
1213
+ ] of delta.tool_calls.entries()) {
1214
+ const index = (_b2 = toolCallDelta.index) != null ? _b2 : fallbackIndex;
1215
+ if (toolCallDelta.type != null) {
1216
+ toolCallTypes.set(index, toolCallDelta.type);
1217
+ }
786
1218
  if (toolCalls[index] == null) {
787
1219
  if (toolCallDelta.id == null) {
788
1220
  throw new InvalidResponseDataError({
@@ -790,7 +1222,7 @@ var MoonshotAIChatLanguageModel = class {
790
1222
  message: `Expected 'id' to be a string.`
791
1223
  });
792
1224
  }
793
- if (((_a2 = toolCallDelta.function) == null ? void 0 : _a2.name) == null) {
1225
+ if (((_c = toolCallDelta.function) == null ? void 0 : _c.name) == null) {
794
1226
  throw new InvalidResponseDataError({
795
1227
  data: toolCallDelta,
796
1228
  message: `Expected 'function.name' to be a string.`
@@ -806,12 +1238,12 @@ var MoonshotAIChatLanguageModel = class {
806
1238
  type: "function",
807
1239
  function: {
808
1240
  name: toolCallDelta.function.name,
809
- arguments: (_b2 = toolCallDelta.function.arguments) != null ? _b2 : ""
1241
+ arguments: (_d = toolCallDelta.function.arguments) != null ? _d : ""
810
1242
  },
811
1243
  hasFinished: false
812
1244
  };
813
1245
  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) {
1246
+ if (((_e = toolCall2.function) == null ? void 0 : _e.name) != null && ((_f = toolCall2.function) == null ? void 0 : _f.arguments) != null && toolCall2.function.arguments.length > 0) {
815
1247
  controller.enqueue({
816
1248
  type: "tool-input-delta",
817
1249
  id: toolCall2.id,
@@ -824,13 +1256,13 @@ var MoonshotAIChatLanguageModel = class {
824
1256
  if (toolCall.hasFinished) {
825
1257
  continue;
826
1258
  }
827
- if (((_e = toolCallDelta.function) == null ? void 0 : _e.arguments) != null) {
1259
+ if (((_g = toolCallDelta.function) == null ? void 0 : _g.arguments) != null) {
828
1260
  toolCall.function.arguments += toolCallDelta.function.arguments;
829
1261
  }
830
1262
  controller.enqueue({
831
1263
  type: "tool-input-delta",
832
1264
  id: toolCall.id,
833
- delta: (_f = toolCallDelta.function.arguments) != null ? _f : ""
1265
+ delta: (_h = toolCallDelta.function.arguments) != null ? _h : ""
834
1266
  });
835
1267
  }
836
1268
  }
@@ -859,7 +1291,22 @@ var MoonshotAIChatLanguageModel = class {
859
1291
  controller.enqueue({
860
1292
  type: "finish",
861
1293
  finishReason,
862
- usage: convertMoonshotAIChatUsage(usage)
1294
+ usage: convertMoonshotAIChatUsage(topLevelUsage != null ? topLevelUsage : choiceUsage),
1295
+ providerMetadata: {
1296
+ [providerOptionsName]: {
1297
+ ...responseObject != null && { responseObject },
1298
+ ...choiceIndex != null && { choiceIndex },
1299
+ ...messageRole != null && { messageRole },
1300
+ ...toolCallTypes.size > 0 && {
1301
+ toolCallTypes: [...toolCallTypes.entries()].sort(([left], [right]) => left - right).map(([, type]) => type)
1302
+ },
1303
+ ...contentLogprobs.length > 0 && {
1304
+ logprobs: {
1305
+ content: contentLogprobs
1306
+ }
1307
+ }
1308
+ }
1309
+ }
863
1310
  });
864
1311
  }
865
1312
  })
@@ -871,13 +1318,12 @@ var MoonshotAIChatLanguageModel = class {
871
1318
  };
872
1319
 
873
1320
  // src/version.ts
874
- var VERSION = true ? "2.0.49" : "0.0.0-test";
1321
+ var VERSION = true ? "2.0.53" : "0.0.0-test";
875
1322
 
876
1323
  // src/moonshotai-provider.ts
877
1324
  var defaultBaseURL = "https://api.moonshot.ai/v1";
878
1325
  function getModelStructuredOutputSupport(modelId) {
879
- if (modelId.startsWith("kimi-k")) return true;
880
- return false;
1326
+ 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
1327
  }
882
1328
  function createMoonshotAI(options = {}) {
883
1329
  var _a;