@ai-sdk/moonshotai 2.0.43 → 2.0.45

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
@@ -7,10 +7,162 @@ import {
7
7
  withoutTrailingSlash,
8
8
  withUserAgentSuffix
9
9
  } from "@ai-sdk/provider-utils";
10
- import { z } from "zod/v4";
11
10
 
12
11
  // src/moonshotai-chat-language-model.ts
13
- import { OpenAICompatibleChatLanguageModel } from "@ai-sdk/openai-compatible";
12
+ import {
13
+ InvalidResponseDataError
14
+ } from "@ai-sdk/provider";
15
+ import {
16
+ combineHeaders,
17
+ createEventSourceResponseHandler,
18
+ createJsonErrorResponseHandler,
19
+ createJsonResponseHandler,
20
+ generateId,
21
+ parseProviderOptions,
22
+ postJsonToApi
23
+ } from "@ai-sdk/provider-utils";
24
+
25
+ // src/convert-to-moonshotai-chat-messages.ts
26
+ import {
27
+ UnsupportedFunctionalityError
28
+ } from "@ai-sdk/provider";
29
+ import {
30
+ convertBase64ToUint8Array,
31
+ convertToBase64
32
+ } from "@ai-sdk/provider-utils";
33
+ function convertToMoonshotAIChatMessages(prompt) {
34
+ var _a;
35
+ const messages = [];
36
+ for (const { role, content } of prompt) {
37
+ switch (role) {
38
+ case "system": {
39
+ messages.push({ role: "system", content });
40
+ break;
41
+ }
42
+ case "user": {
43
+ if (content.length === 1 && content[0].type === "text") {
44
+ messages.push({
45
+ role: "user",
46
+ content: content[0].text
47
+ });
48
+ break;
49
+ }
50
+ messages.push({
51
+ role: "user",
52
+ content: content.map((part) => {
53
+ switch (part.type) {
54
+ case "text": {
55
+ return { type: "text", text: part.text };
56
+ }
57
+ case "file": {
58
+ if (part.mediaType.startsWith("image/")) {
59
+ const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
60
+ return {
61
+ type: "image_url",
62
+ image_url: {
63
+ url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${convertToBase64(part.data)}`
64
+ }
65
+ };
66
+ }
67
+ if (part.mediaType.startsWith("video/")) {
68
+ const mediaType = part.mediaType === "video/*" ? "video/mp4" : part.mediaType;
69
+ return {
70
+ type: "video_url",
71
+ video_url: {
72
+ url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${convertToBase64(part.data)}`
73
+ }
74
+ };
75
+ }
76
+ if (part.mediaType.startsWith("text/")) {
77
+ const textContent = part.data instanceof URL ? part.data.toString() : typeof part.data === "string" ? new TextDecoder().decode(
78
+ convertBase64ToUint8Array(part.data)
79
+ ) : new TextDecoder().decode(part.data);
80
+ return {
81
+ type: "text",
82
+ text: textContent
83
+ };
84
+ }
85
+ throw new UnsupportedFunctionalityError({
86
+ functionality: `file part media type ${part.mediaType}`
87
+ });
88
+ }
89
+ }
90
+ })
91
+ });
92
+ break;
93
+ }
94
+ case "assistant": {
95
+ let text = "";
96
+ let reasoning = "";
97
+ const toolCalls = [];
98
+ for (const part of content) {
99
+ switch (part.type) {
100
+ case "text": {
101
+ text += part.text;
102
+ break;
103
+ }
104
+ case "reasoning": {
105
+ reasoning += part.text;
106
+ break;
107
+ }
108
+ case "tool-call": {
109
+ toolCalls.push({
110
+ id: part.toolCallId,
111
+ type: "function",
112
+ function: {
113
+ name: part.toolName,
114
+ arguments: JSON.stringify(part.input)
115
+ }
116
+ });
117
+ break;
118
+ }
119
+ }
120
+ }
121
+ messages.push({
122
+ role: "assistant",
123
+ content: toolCalls.length > 0 ? text || null : text,
124
+ ...reasoning.length > 0 ? { reasoning_content: reasoning } : {},
125
+ tool_calls: toolCalls.length > 0 ? toolCalls : void 0
126
+ });
127
+ break;
128
+ }
129
+ case "tool": {
130
+ for (const toolResponse of content) {
131
+ if (toolResponse.type === "tool-approval-response") {
132
+ continue;
133
+ }
134
+ const output = toolResponse.output;
135
+ let contentValue;
136
+ switch (output.type) {
137
+ case "text":
138
+ case "error-text":
139
+ contentValue = output.value;
140
+ break;
141
+ case "execution-denied":
142
+ contentValue = (_a = output.reason) != null ? _a : "Tool call execution denied.";
143
+ break;
144
+ case "content":
145
+ case "json":
146
+ case "error-json":
147
+ contentValue = JSON.stringify(output.value);
148
+ break;
149
+ }
150
+ messages.push({
151
+ role: "tool",
152
+ tool_call_id: toolResponse.toolCallId,
153
+ content: contentValue
154
+ });
155
+ }
156
+ break;
157
+ }
158
+ default: {
159
+ const _exhaustiveCheck = role;
160
+ throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
161
+ }
162
+ }
163
+ }
164
+ return messages;
165
+ }
14
166
 
15
167
  // src/convert-moonshotai-chat-usage.ts
16
168
  function convertMoonshotAIChatUsage(usage) {
@@ -51,56 +203,592 @@ function convertMoonshotAIChatUsage(usage) {
51
203
  };
52
204
  }
53
205
 
206
+ // src/get-response-metadata.ts
207
+ function getResponseMetadata({
208
+ id,
209
+ model,
210
+ created
211
+ }) {
212
+ return {
213
+ id: id != null ? id : void 0,
214
+ modelId: model != null ? model : void 0,
215
+ timestamp: created != null ? new Date(created * 1e3) : void 0
216
+ };
217
+ }
218
+
219
+ // src/map-moonshotai-finish-reason.ts
220
+ function mapMoonshotAIFinishReason(finishReason) {
221
+ switch (finishReason) {
222
+ case "stop":
223
+ return "stop";
224
+ case "length":
225
+ return "length";
226
+ case "content_filter":
227
+ return "content-filter";
228
+ case "function_call":
229
+ case "tool_calls":
230
+ return "tool-calls";
231
+ default:
232
+ return "other";
233
+ }
234
+ }
235
+
236
+ // src/moonshotai-chat-api-types.ts
237
+ 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()
246
+ }).nullish(),
247
+ completion_tokens_details: z.object({
248
+ reasoning_tokens: z.number().nullish()
249
+ }).nullish()
250
+ }).nullish();
251
+ var moonshotAIErrorSchema = z.object({
252
+ error: z.object({
253
+ message: z.string(),
254
+ type: z.string().nullish()
255
+ })
256
+ });
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()
273
+ })
274
+ })
275
+ ).nullish()
276
+ }),
277
+ finish_reason: z.string().nullish()
278
+ })
279
+ ),
280
+ usage: tokenUsageSchema
281
+ });
282
+ var moonshotAIChatChunkSchema = lazySchema(
283
+ () => 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()
302
+ })
303
+ })
304
+ ).nullish()
305
+ }).nullish(),
306
+ finish_reason: z.string().nullish()
307
+ })
308
+ ),
309
+ usage: tokenUsageSchema
310
+ }),
311
+ moonshotAIErrorSchema
312
+ ])
313
+ )
314
+ );
315
+
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 UnsupportedFunctionalityError2
346
+ } from "@ai-sdk/provider";
347
+ function prepareTools({
348
+ tools,
349
+ toolChoice
350
+ }) {
351
+ tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
352
+ const toolWarnings = [];
353
+ if (tools == null) {
354
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
355
+ }
356
+ const moonshotTools = [];
357
+ for (const tool of tools) {
358
+ if (tool.type === "provider") {
359
+ toolWarnings.push({
360
+ type: "unsupported",
361
+ feature: `provider-defined tool ${tool.id}`
362
+ });
363
+ } else {
364
+ moonshotTools.push({
365
+ type: "function",
366
+ function: {
367
+ name: tool.name,
368
+ description: tool.description,
369
+ parameters: tool.inputSchema,
370
+ ...tool.strict != null ? { strict: tool.strict } : {}
371
+ }
372
+ });
373
+ }
374
+ }
375
+ if (toolChoice == null) {
376
+ return { tools: moonshotTools, toolChoice: void 0, toolWarnings };
377
+ }
378
+ const type = toolChoice.type;
379
+ switch (type) {
380
+ case "auto":
381
+ case "none":
382
+ case "required":
383
+ return { tools: moonshotTools, toolChoice: type, toolWarnings };
384
+ case "tool":
385
+ return {
386
+ tools: moonshotTools,
387
+ toolChoice: {
388
+ type: "function",
389
+ function: { name: toolChoice.toolName }
390
+ },
391
+ toolWarnings
392
+ };
393
+ default: {
394
+ const _exhaustiveCheck = type;
395
+ throw new UnsupportedFunctionalityError2({
396
+ functionality: `tool choice type: ${_exhaustiveCheck}`
397
+ });
398
+ }
399
+ }
400
+ }
401
+
54
402
  // src/moonshotai-chat-language-model.ts
55
- var MoonshotAIChatLanguageModel = class extends OpenAICompatibleChatLanguageModel {
403
+ var MoonshotAIChatLanguageModel = class {
56
404
  constructor(modelId, config) {
57
- super(modelId, config);
405
+ this.specificationVersion = "v3";
406
+ // Moonshot AI does not fetch external URLs; the AI SDK downloads and
407
+ // inlines URL file parts instead. ms:// file references from the Moonshot
408
+ // Files API are passed through natively.
409
+ this.supportedUrls = {
410
+ "image/*": [/^ms:\/\//],
411
+ "video/*": [/^ms:\/\//]
412
+ };
413
+ this.modelId = modelId;
414
+ this.config = config;
415
+ this.failedResponseHandler = createJsonErrorResponseHandler({
416
+ errorSchema: moonshotAIErrorSchema,
417
+ errorToMessage: (error) => error.error.message
418
+ });
58
419
  }
59
- async doGenerate(options) {
420
+ get provider() {
421
+ return this.config.provider;
422
+ }
423
+ get providerOptionsName() {
424
+ return this.config.provider.split(".")[0].trim();
425
+ }
426
+ async getArgs({
427
+ prompt,
428
+ maxOutputTokens,
429
+ temperature,
430
+ topP,
431
+ topK,
432
+ frequencyPenalty,
433
+ presencePenalty,
434
+ providerOptions,
435
+ stopSequences,
436
+ responseFormat,
437
+ seed,
438
+ toolChoice,
439
+ tools
440
+ }) {
60
441
  var _a, _b;
61
- const result = await super.doGenerate(options);
62
- const usage = (_b = (_a = result.response) == null ? void 0 : _a.body) == null ? void 0 : _b.usage;
442
+ const moonshotOptions = (_a = await parseProviderOptions({
443
+ provider: this.providerOptionsName,
444
+ providerOptions,
445
+ schema: moonshotaiLanguageModelOptions
446
+ })) != null ? _a : {};
447
+ const messages = convertToMoonshotAIChatMessages(prompt);
448
+ const allWarnings = [];
449
+ if (topK != null) {
450
+ allWarnings.push({ type: "unsupported", feature: "topK" });
451
+ }
452
+ if (seed != null) {
453
+ allWarnings.push({ type: "unsupported", feature: "seed" });
454
+ }
455
+ const {
456
+ tools: moonshotTools,
457
+ toolChoice: moonshotToolChoice,
458
+ toolWarnings
459
+ } = prepareTools({ tools, toolChoice });
460
+ const thinking = moonshotOptions.thinking;
461
+ let keep;
462
+ if (moonshotOptions.reasoningHistory === "preserved") {
463
+ if (getModelThinkingKeepSupport(this.modelId)) {
464
+ keep = "all";
465
+ } else {
466
+ allWarnings.push({
467
+ type: "unsupported",
468
+ feature: `reasoningHistory 'preserved' is not supported by model "${this.modelId}"`
469
+ });
470
+ }
471
+ }
472
+ let response_format;
473
+ if ((responseFormat == null ? void 0 : responseFormat.type) === "json") {
474
+ if (this.config.supportsStructuredOutputs === true && responseFormat.schema != null) {
475
+ const { $schema: _$schema, ...schemaWithoutDollarSchema } = responseFormat.schema;
476
+ response_format = {
477
+ type: "json_schema",
478
+ json_schema: {
479
+ name: (_b = responseFormat.name) != null ? _b : "response",
480
+ schema: schemaWithoutDollarSchema,
481
+ ...responseFormat.description != null && {
482
+ description: responseFormat.description
483
+ }
484
+ }
485
+ };
486
+ } else {
487
+ response_format = { type: "json_object" };
488
+ }
489
+ }
490
+ return {
491
+ args: {
492
+ model: this.modelId,
493
+ max_tokens: maxOutputTokens,
494
+ temperature,
495
+ top_p: topP,
496
+ frequency_penalty: frequencyPenalty,
497
+ presence_penalty: presencePenalty,
498
+ response_format,
499
+ stop: stopSequences,
500
+ messages,
501
+ tools: moonshotTools,
502
+ tool_choice: moonshotToolChoice,
503
+ ...thinking != null || keep != null ? {
504
+ thinking: {
505
+ ...(thinking == null ? void 0 : thinking.type) != null && { type: thinking.type },
506
+ ...(thinking == null ? void 0 : thinking.budgetTokens) !== void 0 && {
507
+ budget_tokens: thinking.budgetTokens
508
+ },
509
+ ...keep != null && { keep }
510
+ }
511
+ } : {},
512
+ ...moonshotOptions.reasoningEffort != null && {
513
+ reasoning_effort: moonshotOptions.reasoningEffort
514
+ },
515
+ ...moonshotOptions.promptCacheKey != null && {
516
+ prompt_cache_key: moonshotOptions.promptCacheKey
517
+ },
518
+ ...moonshotOptions.safetyIdentifier != null && {
519
+ safety_identifier: moonshotOptions.safetyIdentifier
520
+ }
521
+ },
522
+ warnings: [...allWarnings, ...toolWarnings]
523
+ };
524
+ }
525
+ async doGenerate(options) {
526
+ var _a, _b, _c, _d, _e;
527
+ const { args, warnings } = await this.getArgs({ ...options });
528
+ const {
529
+ responseHeaders,
530
+ value: responseBody,
531
+ rawValue: rawResponse
532
+ } = await postJsonToApi({
533
+ url: this.config.url({
534
+ path: "/chat/completions",
535
+ modelId: this.modelId
536
+ }),
537
+ headers: combineHeaders((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
538
+ body: args,
539
+ failedResponseHandler: this.failedResponseHandler,
540
+ successfulResponseHandler: createJsonResponseHandler(
541
+ moonshotAIChatResponseSchema
542
+ ),
543
+ abortSignal: options.abortSignal,
544
+ fetch: this.config.fetch
545
+ });
546
+ const choice = responseBody.choices[0];
547
+ const content = [];
548
+ const reasoning = choice.message.reasoning_content;
549
+ if (reasoning != null && reasoning.length > 0) {
550
+ content.push({ type: "reasoning", text: reasoning });
551
+ }
552
+ if (choice.message.tool_calls != null) {
553
+ for (const toolCall of choice.message.tool_calls) {
554
+ content.push({
555
+ type: "tool-call",
556
+ toolCallId: (_c = toolCall.id) != null ? _c : generateId(),
557
+ toolName: toolCall.function.name,
558
+ input: (_d = toolCall.function.arguments) != null ? _d : ""
559
+ });
560
+ }
561
+ }
562
+ const text = choice.message.content;
563
+ if (text != null && text.length > 0) {
564
+ content.push({ type: "text", text });
565
+ }
63
566
  return {
64
- ...result,
65
- usage: convertMoonshotAIChatUsage(usage)
567
+ content,
568
+ finishReason: {
569
+ unified: mapMoonshotAIFinishReason(choice.finish_reason),
570
+ raw: (_e = choice.finish_reason) != null ? _e : void 0
571
+ },
572
+ usage: convertMoonshotAIChatUsage(responseBody.usage),
573
+ request: { body: args },
574
+ response: {
575
+ ...getResponseMetadata(responseBody),
576
+ headers: responseHeaders,
577
+ body: rawResponse
578
+ },
579
+ warnings
66
580
  };
67
581
  }
68
582
  async doStream(options) {
69
- const result = await super.doStream(options);
583
+ var _a, _b;
584
+ const { args, warnings } = await this.getArgs({ ...options });
585
+ const body = {
586
+ ...args,
587
+ stream: true,
588
+ ...this.config.includeUsage && {
589
+ stream_options: { include_usage: true }
590
+ }
591
+ };
592
+ const { responseHeaders, value: response } = await postJsonToApi({
593
+ url: this.config.url({
594
+ path: "/chat/completions",
595
+ modelId: this.modelId
596
+ }),
597
+ headers: combineHeaders((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
598
+ body,
599
+ failedResponseHandler: this.failedResponseHandler,
600
+ successfulResponseHandler: createEventSourceResponseHandler(
601
+ moonshotAIChatChunkSchema
602
+ ),
603
+ abortSignal: options.abortSignal,
604
+ fetch: this.config.fetch
605
+ });
606
+ const toolCalls = [];
607
+ let finishReason = {
608
+ unified: "other",
609
+ raw: void 0
610
+ };
611
+ let usage = void 0;
612
+ let isFirstChunk = true;
613
+ let isActiveReasoning = false;
614
+ let isActiveText = false;
70
615
  return {
71
- ...result,
72
- stream: result.stream.pipeThrough(
616
+ stream: response.pipeThrough(
73
617
  new TransformStream({
618
+ start(controller) {
619
+ controller.enqueue({ type: "stream-start", warnings });
620
+ },
74
621
  transform(chunk, controller) {
75
- if (chunk.type === "finish" && chunk.usage) {
622
+ var _a2, _b2, _c, _d, _e, _f;
623
+ if (options.includeRawChunks) {
624
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
625
+ }
626
+ if (!chunk.success) {
627
+ finishReason = { unified: "error", raw: void 0 };
628
+ controller.enqueue({ type: "error", error: chunk.error });
629
+ return;
630
+ }
631
+ const value = chunk.value;
632
+ if ("error" in value) {
633
+ finishReason = { unified: "error", raw: void 0 };
634
+ controller.enqueue({ type: "error", error: value.error.message });
635
+ return;
636
+ }
637
+ if (isFirstChunk) {
638
+ isFirstChunk = false;
639
+ controller.enqueue({
640
+ type: "response-metadata",
641
+ ...getResponseMetadata(value)
642
+ });
643
+ }
644
+ if (value.usage != null) {
645
+ usage = value.usage;
646
+ }
647
+ const choice = value.choices[0];
648
+ if ((choice == null ? void 0 : choice.finish_reason) != null) {
649
+ finishReason = {
650
+ unified: mapMoonshotAIFinishReason(choice.finish_reason),
651
+ raw: choice.finish_reason
652
+ };
653
+ }
654
+ if ((choice == null ? void 0 : choice.delta) == null) {
655
+ return;
656
+ }
657
+ const delta = choice.delta;
658
+ const reasoningContent = delta.reasoning_content;
659
+ if (reasoningContent) {
660
+ if (!isActiveReasoning) {
661
+ controller.enqueue({
662
+ type: "reasoning-start",
663
+ id: "reasoning-0"
664
+ });
665
+ isActiveReasoning = true;
666
+ }
667
+ controller.enqueue({
668
+ type: "reasoning-delta",
669
+ id: "reasoning-0",
670
+ delta: reasoningContent
671
+ });
672
+ }
673
+ if (delta.content) {
674
+ if (!isActiveText) {
675
+ controller.enqueue({ type: "text-start", id: "txt-0" });
676
+ isActiveText = true;
677
+ }
678
+ if (isActiveReasoning) {
679
+ controller.enqueue({
680
+ type: "reasoning-end",
681
+ id: "reasoning-0"
682
+ });
683
+ isActiveReasoning = false;
684
+ }
76
685
  controller.enqueue({
77
- ...chunk,
78
- usage: convertMoonshotAIChatUsage(chunk.usage.raw)
686
+ type: "text-delta",
687
+ id: "txt-0",
688
+ delta: delta.content
79
689
  });
80
- } else {
81
- controller.enqueue(chunk);
82
690
  }
691
+ if (delta.tool_calls != null) {
692
+ if (isActiveReasoning) {
693
+ controller.enqueue({
694
+ type: "reasoning-end",
695
+ id: "reasoning-0"
696
+ });
697
+ isActiveReasoning = false;
698
+ }
699
+ for (const toolCallDelta of delta.tool_calls) {
700
+ const index = toolCallDelta.index;
701
+ if (toolCalls[index] == null) {
702
+ if (toolCallDelta.id == null) {
703
+ throw new InvalidResponseDataError({
704
+ data: toolCallDelta,
705
+ message: `Expected 'id' to be a string.`
706
+ });
707
+ }
708
+ if (((_a2 = toolCallDelta.function) == null ? void 0 : _a2.name) == null) {
709
+ throw new InvalidResponseDataError({
710
+ data: toolCallDelta,
711
+ message: `Expected 'function.name' to be a string.`
712
+ });
713
+ }
714
+ controller.enqueue({
715
+ type: "tool-input-start",
716
+ id: toolCallDelta.id,
717
+ toolName: toolCallDelta.function.name
718
+ });
719
+ toolCalls[index] = {
720
+ id: toolCallDelta.id,
721
+ type: "function",
722
+ function: {
723
+ name: toolCallDelta.function.name,
724
+ arguments: (_b2 = toolCallDelta.function.arguments) != null ? _b2 : ""
725
+ },
726
+ hasFinished: false
727
+ };
728
+ const toolCall2 = toolCalls[index];
729
+ if (((_c = toolCall2.function) == null ? void 0 : _c.name) != null && ((_d = toolCall2.function) == null ? void 0 : _d.arguments) != null && toolCall2.function.arguments.length > 0) {
730
+ controller.enqueue({
731
+ type: "tool-input-delta",
732
+ id: toolCall2.id,
733
+ delta: toolCall2.function.arguments
734
+ });
735
+ }
736
+ continue;
737
+ }
738
+ const toolCall = toolCalls[index];
739
+ if (toolCall.hasFinished) {
740
+ continue;
741
+ }
742
+ if (((_e = toolCallDelta.function) == null ? void 0 : _e.arguments) != null) {
743
+ toolCall.function.arguments += toolCallDelta.function.arguments;
744
+ }
745
+ controller.enqueue({
746
+ type: "tool-input-delta",
747
+ id: toolCall.id,
748
+ delta: (_f = toolCallDelta.function.arguments) != null ? _f : ""
749
+ });
750
+ }
751
+ }
752
+ },
753
+ flush(controller) {
754
+ if (isActiveReasoning) {
755
+ controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
756
+ }
757
+ if (isActiveText) {
758
+ controller.enqueue({ type: "text-end", id: "txt-0" });
759
+ }
760
+ for (const toolCall of toolCalls.filter(
761
+ (toolCall2) => !toolCall2.hasFinished
762
+ )) {
763
+ controller.enqueue({
764
+ type: "tool-input-end",
765
+ id: toolCall.id
766
+ });
767
+ controller.enqueue({
768
+ type: "tool-call",
769
+ toolCallId: toolCall.id,
770
+ toolName: toolCall.function.name,
771
+ input: toolCall.function.arguments
772
+ });
773
+ }
774
+ controller.enqueue({
775
+ type: "finish",
776
+ finishReason,
777
+ usage: convertMoonshotAIChatUsage(usage)
778
+ });
83
779
  }
84
780
  })
85
- )
781
+ ),
782
+ request: { body },
783
+ response: { headers: responseHeaders }
86
784
  };
87
785
  }
88
786
  };
89
787
 
90
788
  // src/version.ts
91
- var VERSION = true ? "2.0.43" : "0.0.0-test";
789
+ var VERSION = true ? "2.0.45" : "0.0.0-test";
92
790
 
93
791
  // src/moonshotai-provider.ts
94
- var moonshotaiErrorSchema = z.object({
95
- error: z.object({
96
- message: z.string(),
97
- type: z.string().nullish()
98
- })
99
- });
100
- var moonshotaiErrorStructure = {
101
- errorSchema: moonshotaiErrorSchema,
102
- errorToMessage: (data) => data.error.message
103
- };
104
792
  var defaultBaseURL = "https://api.moonshot.ai/v1";
105
793
  function getModelStructuredOutputSupport(modelId) {
106
794
  if (modelId.startsWith("kimi-k")) return true;
@@ -120,49 +808,14 @@ function createMoonshotAI(options = {}) {
120
808
  },
121
809
  `ai-sdk/moonshotai/${VERSION}`
122
810
  );
123
- const getCommonModelConfig = (modelType) => ({
124
- provider: `moonshotai.${modelType}`,
125
- url: ({ path }) => `${baseURL}${path}`,
126
- headers: getHeaders,
127
- fetch: options.fetch
128
- });
129
811
  const createChatModel = (modelId) => {
130
812
  return new MoonshotAIChatLanguageModel(modelId, {
131
- ...getCommonModelConfig("chat"),
813
+ provider: "moonshotai.chat",
814
+ url: ({ path }) => `${baseURL}${path}`,
815
+ headers: getHeaders,
816
+ fetch: options.fetch,
132
817
  includeUsage: true,
133
- errorStructure: moonshotaiErrorStructure,
134
- supportsStructuredOutputs: getModelStructuredOutputSupport(modelId),
135
- transformRequestBody: (args) => {
136
- var _a2, _b;
137
- const thinking = args.thinking;
138
- const reasoningHistory = args.reasoningHistory;
139
- const { thinking: _, reasoningHistory: __, ...rest } = args;
140
- const schema = (_b = (_a2 = rest.response_format) == null ? void 0 : _a2.json_schema) == null ? void 0 : _b.schema;
141
- if (schema != null) {
142
- const { $schema: _$schema, ...schemaWithoutDollarSchema } = schema;
143
- rest.response_format = {
144
- ...rest.response_format,
145
- json_schema: {
146
- ...rest.response_format.json_schema,
147
- schema: schemaWithoutDollarSchema
148
- }
149
- };
150
- }
151
- return {
152
- ...rest,
153
- ...thinking && {
154
- thinking: {
155
- type: thinking.type,
156
- ...thinking.budgetTokens !== void 0 && {
157
- budget_tokens: thinking.budgetTokens
158
- }
159
- }
160
- },
161
- ...reasoningHistory && {
162
- reasoning_history: reasoningHistory
163
- }
164
- };
165
- }
818
+ supportsStructuredOutputs: getModelStructuredOutputSupport(modelId)
166
819
  });
167
820
  };
168
821
  const provider = (modelId) => createChatModel(modelId);