@ai-sdk/moonshotai 3.0.31 → 3.0.32
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 +7 -1
- package/dist/index.js +683 -70
- package/dist/index.js.map +1 -1
- package/package.json +5 -6
- package/src/convert-to-moonshotai-chat-messages.ts +197 -0
- package/src/map-moonshotai-finish-reason.ts +19 -0
- package/src/moonshotai-chat-api-types.ts +161 -0
- package/src/moonshotai-chat-language-model.ts +454 -29
- package/src/moonshotai-chat-options.ts +29 -2
- package/src/moonshotai-prepare-tools.ts +98 -0
- package/src/moonshotai-provider.ts +5 -70
package/dist/index.js
CHANGED
|
@@ -7,16 +7,183 @@ 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";
|
|
14
12
|
import {
|
|
13
|
+
combineHeaders,
|
|
14
|
+
createEventSourceResponseHandler,
|
|
15
|
+
createJsonErrorResponseHandler,
|
|
16
|
+
createJsonResponseHandler,
|
|
17
|
+
createLanguageModelResponseMetadata as getResponseMetadata,
|
|
18
|
+
generateId,
|
|
19
|
+
isCustomReasoning,
|
|
20
|
+
mapReasoningToProviderEffort,
|
|
21
|
+
parseProviderOptions,
|
|
22
|
+
postJsonToApi,
|
|
15
23
|
serializeModelOptions,
|
|
24
|
+
StreamingToolCallTracker,
|
|
16
25
|
WORKFLOW_SERIALIZE,
|
|
17
26
|
WORKFLOW_DESERIALIZE
|
|
18
27
|
} from "@ai-sdk/provider-utils";
|
|
19
28
|
|
|
29
|
+
// src/convert-to-moonshotai-chat-messages.ts
|
|
30
|
+
import {
|
|
31
|
+
UnsupportedFunctionalityError
|
|
32
|
+
} from "@ai-sdk/provider";
|
|
33
|
+
import {
|
|
34
|
+
convertBase64ToUint8Array,
|
|
35
|
+
convertToBase64,
|
|
36
|
+
getTopLevelMediaType,
|
|
37
|
+
resolveFullMediaType
|
|
38
|
+
} from "@ai-sdk/provider-utils";
|
|
39
|
+
function convertToMoonshotAIChatMessages(prompt) {
|
|
40
|
+
var _a;
|
|
41
|
+
const messages = [];
|
|
42
|
+
for (const { role, content } of prompt) {
|
|
43
|
+
switch (role) {
|
|
44
|
+
case "system": {
|
|
45
|
+
messages.push({ role: "system", content });
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
case "user": {
|
|
49
|
+
if (content.length === 1 && content[0].type === "text") {
|
|
50
|
+
messages.push({
|
|
51
|
+
role: "user",
|
|
52
|
+
content: content[0].text
|
|
53
|
+
});
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
messages.push({
|
|
57
|
+
role: "user",
|
|
58
|
+
content: content.map((part) => {
|
|
59
|
+
switch (part.type) {
|
|
60
|
+
case "text": {
|
|
61
|
+
return { type: "text", text: part.text };
|
|
62
|
+
}
|
|
63
|
+
case "file": {
|
|
64
|
+
switch (part.data.type) {
|
|
65
|
+
case "reference": {
|
|
66
|
+
throw new UnsupportedFunctionalityError({
|
|
67
|
+
functionality: "file parts with provider references"
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
case "text": {
|
|
71
|
+
throw new UnsupportedFunctionalityError({
|
|
72
|
+
functionality: "text file parts"
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
case "url":
|
|
76
|
+
case "data": {
|
|
77
|
+
const topLevel = getTopLevelMediaType(part.mediaType);
|
|
78
|
+
if (topLevel === "image") {
|
|
79
|
+
return {
|
|
80
|
+
type: "image_url",
|
|
81
|
+
image_url: {
|
|
82
|
+
url: part.data.type === "url" ? part.data.url.toString() : `data:${resolveFullMediaType({ part })};base64,${convertToBase64(part.data.data)}`
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
if (topLevel === "video") {
|
|
87
|
+
return {
|
|
88
|
+
type: "video_url",
|
|
89
|
+
video_url: {
|
|
90
|
+
url: part.data.type === "url" ? part.data.url.toString() : `data:${resolveFullMediaType({ part })};base64,${convertToBase64(part.data.data)}`
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
if (topLevel === "text") {
|
|
95
|
+
const textContent = part.data.type === "url" ? part.data.url.toString() : typeof part.data.data === "string" ? new TextDecoder().decode(
|
|
96
|
+
convertBase64ToUint8Array(part.data.data)
|
|
97
|
+
) : new TextDecoder().decode(part.data.data);
|
|
98
|
+
return {
|
|
99
|
+
type: "text",
|
|
100
|
+
text: textContent
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
throw new UnsupportedFunctionalityError({
|
|
104
|
+
functionality: `file part media type ${part.mediaType}`
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
})
|
|
111
|
+
});
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case "assistant": {
|
|
115
|
+
let text = "";
|
|
116
|
+
let reasoning = "";
|
|
117
|
+
const toolCalls = [];
|
|
118
|
+
for (const part of content) {
|
|
119
|
+
switch (part.type) {
|
|
120
|
+
case "text": {
|
|
121
|
+
text += part.text;
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
case "reasoning": {
|
|
125
|
+
reasoning += part.text;
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
case "tool-call": {
|
|
129
|
+
toolCalls.push({
|
|
130
|
+
id: part.toolCallId,
|
|
131
|
+
type: "function",
|
|
132
|
+
function: {
|
|
133
|
+
name: part.toolName,
|
|
134
|
+
arguments: JSON.stringify(part.input)
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
messages.push({
|
|
142
|
+
role: "assistant",
|
|
143
|
+
content: toolCalls.length > 0 ? text || null : text,
|
|
144
|
+
...reasoning.length > 0 ? { reasoning_content: reasoning } : {},
|
|
145
|
+
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
|
|
146
|
+
});
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
case "tool": {
|
|
150
|
+
for (const toolResponse of content) {
|
|
151
|
+
if (toolResponse.type === "tool-approval-response") {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
const output = toolResponse.output;
|
|
155
|
+
let contentValue;
|
|
156
|
+
switch (output.type) {
|
|
157
|
+
case "text":
|
|
158
|
+
case "error-text":
|
|
159
|
+
contentValue = output.value;
|
|
160
|
+
break;
|
|
161
|
+
case "execution-denied":
|
|
162
|
+
contentValue = (_a = output.reason) != null ? _a : "Tool call execution denied.";
|
|
163
|
+
break;
|
|
164
|
+
case "content":
|
|
165
|
+
case "json":
|
|
166
|
+
case "error-json":
|
|
167
|
+
contentValue = JSON.stringify(output.value);
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
messages.push({
|
|
171
|
+
role: "tool",
|
|
172
|
+
tool_call_id: toolResponse.toolCallId,
|
|
173
|
+
content: contentValue
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
default: {
|
|
179
|
+
const _exhaustiveCheck = role;
|
|
180
|
+
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return messages;
|
|
185
|
+
}
|
|
186
|
+
|
|
20
187
|
// src/convert-moonshotai-chat-usage.ts
|
|
21
188
|
import { createNullLanguageModelUsage } from "@ai-sdk/provider-utils";
|
|
22
189
|
function convertMoonshotAIChatUsage(usage) {
|
|
@@ -44,8 +211,207 @@ function convertMoonshotAIChatUsage(usage) {
|
|
|
44
211
|
};
|
|
45
212
|
}
|
|
46
213
|
|
|
214
|
+
// src/map-moonshotai-finish-reason.ts
|
|
215
|
+
function mapMoonshotAIFinishReason(finishReason) {
|
|
216
|
+
switch (finishReason) {
|
|
217
|
+
case "stop":
|
|
218
|
+
return "stop";
|
|
219
|
+
case "length":
|
|
220
|
+
return "length";
|
|
221
|
+
case "content_filter":
|
|
222
|
+
return "content-filter";
|
|
223
|
+
case "function_call":
|
|
224
|
+
case "tool_calls":
|
|
225
|
+
return "tool-calls";
|
|
226
|
+
default:
|
|
227
|
+
return "other";
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// src/moonshotai-chat-api-types.ts
|
|
232
|
+
import { lazySchema, zodSchema } from "@ai-sdk/provider-utils";
|
|
233
|
+
import { z } from "zod/v4";
|
|
234
|
+
var tokenUsageSchema = z.object({
|
|
235
|
+
prompt_tokens: z.number().nullish(),
|
|
236
|
+
completion_tokens: z.number().nullish(),
|
|
237
|
+
cached_tokens: z.number().nullish(),
|
|
238
|
+
total_tokens: z.number().nullish(),
|
|
239
|
+
prompt_tokens_details: z.object({
|
|
240
|
+
cached_tokens: z.number().nullish()
|
|
241
|
+
}).nullish(),
|
|
242
|
+
completion_tokens_details: z.object({
|
|
243
|
+
reasoning_tokens: z.number().nullish()
|
|
244
|
+
}).nullish()
|
|
245
|
+
}).nullish();
|
|
246
|
+
var moonshotAIErrorSchema = z.object({
|
|
247
|
+
error: z.object({
|
|
248
|
+
message: z.string(),
|
|
249
|
+
type: z.string().nullish()
|
|
250
|
+
})
|
|
251
|
+
});
|
|
252
|
+
var moonshotAIChatResponseSchema = z.object({
|
|
253
|
+
id: z.string().nullish(),
|
|
254
|
+
created: z.number().nullish(),
|
|
255
|
+
model: z.string().nullish(),
|
|
256
|
+
choices: z.array(
|
|
257
|
+
z.object({
|
|
258
|
+
message: z.object({
|
|
259
|
+
role: z.literal("assistant").nullish(),
|
|
260
|
+
content: z.string().nullish(),
|
|
261
|
+
reasoning_content: z.string().nullish(),
|
|
262
|
+
tool_calls: z.array(
|
|
263
|
+
z.object({
|
|
264
|
+
id: z.string().nullish(),
|
|
265
|
+
function: z.object({
|
|
266
|
+
name: z.string(),
|
|
267
|
+
arguments: z.string()
|
|
268
|
+
})
|
|
269
|
+
})
|
|
270
|
+
).nullish()
|
|
271
|
+
}),
|
|
272
|
+
finish_reason: z.string().nullish()
|
|
273
|
+
})
|
|
274
|
+
),
|
|
275
|
+
usage: tokenUsageSchema
|
|
276
|
+
});
|
|
277
|
+
var moonshotAIChatChunkSchema = lazySchema(
|
|
278
|
+
() => zodSchema(
|
|
279
|
+
z.union([
|
|
280
|
+
z.object({
|
|
281
|
+
id: z.string().nullish(),
|
|
282
|
+
created: z.number().nullish(),
|
|
283
|
+
model: z.string().nullish(),
|
|
284
|
+
choices: z.array(
|
|
285
|
+
z.object({
|
|
286
|
+
delta: z.object({
|
|
287
|
+
role: z.literal("assistant").nullish(),
|
|
288
|
+
content: z.string().nullish(),
|
|
289
|
+
reasoning_content: z.string().nullish(),
|
|
290
|
+
tool_calls: z.array(
|
|
291
|
+
z.object({
|
|
292
|
+
index: z.number(),
|
|
293
|
+
id: z.string().nullish(),
|
|
294
|
+
function: z.object({
|
|
295
|
+
name: z.string().nullish(),
|
|
296
|
+
arguments: z.string().nullish()
|
|
297
|
+
})
|
|
298
|
+
})
|
|
299
|
+
).nullish()
|
|
300
|
+
}).nullish(),
|
|
301
|
+
finish_reason: z.string().nullish()
|
|
302
|
+
})
|
|
303
|
+
),
|
|
304
|
+
usage: tokenUsageSchema
|
|
305
|
+
}),
|
|
306
|
+
moonshotAIErrorSchema
|
|
307
|
+
])
|
|
308
|
+
)
|
|
309
|
+
);
|
|
310
|
+
|
|
311
|
+
// src/moonshotai-chat-options.ts
|
|
312
|
+
import { z as z2 } from "zod/v4";
|
|
313
|
+
var moonshotaiLanguageModelOptions = z2.object({
|
|
314
|
+
/**
|
|
315
|
+
* Reasoning effort for Kimi K3.
|
|
316
|
+
*/
|
|
317
|
+
reasoningEffort: z2.enum(["low", "high", "max"]).optional(),
|
|
318
|
+
thinking: z2.object({
|
|
319
|
+
type: z2.enum(["enabled", "disabled"]).optional(),
|
|
320
|
+
budgetTokens: z2.number().int().min(1024).optional()
|
|
321
|
+
}).optional(),
|
|
322
|
+
reasoningHistory: z2.enum(["disabled", "interleaved", "preserved"]).optional(),
|
|
323
|
+
/**
|
|
324
|
+
* Used to cache responses for similar requests to optimize cache hit rates.
|
|
325
|
+
* Typically a session or task id.
|
|
326
|
+
*/
|
|
327
|
+
promptCacheKey: z2.string().optional(),
|
|
328
|
+
/**
|
|
329
|
+
* A stable identifier used to help Moonshot detect users violating usage
|
|
330
|
+
* policies. Recommended to hash the username or email address.
|
|
331
|
+
*/
|
|
332
|
+
safetyIdentifier: z2.string().optional()
|
|
333
|
+
});
|
|
334
|
+
function getModelThinkingKeepSupport(modelId) {
|
|
335
|
+
return modelId === "kimi-k2.6" || modelId === "kimi-k3" || modelId.startsWith("kimi-k2.7-code");
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// src/moonshotai-prepare-tools.ts
|
|
339
|
+
import {
|
|
340
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
341
|
+
} from "@ai-sdk/provider";
|
|
342
|
+
function prepareTools({
|
|
343
|
+
tools,
|
|
344
|
+
toolChoice
|
|
345
|
+
}) {
|
|
346
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
347
|
+
const toolWarnings = [];
|
|
348
|
+
if (tools == null) {
|
|
349
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
350
|
+
}
|
|
351
|
+
const moonshotTools = [];
|
|
352
|
+
for (const tool of tools) {
|
|
353
|
+
if (tool.type === "provider") {
|
|
354
|
+
toolWarnings.push({
|
|
355
|
+
type: "unsupported",
|
|
356
|
+
feature: `provider-defined tool ${tool.id}`
|
|
357
|
+
});
|
|
358
|
+
} else {
|
|
359
|
+
moonshotTools.push({
|
|
360
|
+
type: "function",
|
|
361
|
+
function: {
|
|
362
|
+
name: tool.name,
|
|
363
|
+
description: tool.description,
|
|
364
|
+
parameters: tool.inputSchema,
|
|
365
|
+
...tool.strict != null ? { strict: tool.strict } : {}
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
if (toolChoice == null) {
|
|
371
|
+
return { tools: moonshotTools, toolChoice: void 0, toolWarnings };
|
|
372
|
+
}
|
|
373
|
+
const type = toolChoice.type;
|
|
374
|
+
switch (type) {
|
|
375
|
+
case "auto":
|
|
376
|
+
case "none":
|
|
377
|
+
case "required":
|
|
378
|
+
return { tools: moonshotTools, toolChoice: type, toolWarnings };
|
|
379
|
+
case "tool":
|
|
380
|
+
return {
|
|
381
|
+
tools: moonshotTools,
|
|
382
|
+
toolChoice: {
|
|
383
|
+
type: "function",
|
|
384
|
+
function: { name: toolChoice.toolName }
|
|
385
|
+
},
|
|
386
|
+
toolWarnings
|
|
387
|
+
};
|
|
388
|
+
default: {
|
|
389
|
+
const _exhaustiveCheck = type;
|
|
390
|
+
throw new UnsupportedFunctionalityError2({
|
|
391
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
47
397
|
// src/moonshotai-chat-language-model.ts
|
|
48
|
-
var MoonshotAIChatLanguageModel = class _MoonshotAIChatLanguageModel
|
|
398
|
+
var MoonshotAIChatLanguageModel = class _MoonshotAIChatLanguageModel {
|
|
399
|
+
constructor(modelId, config) {
|
|
400
|
+
this.specificationVersion = "v4";
|
|
401
|
+
// Moonshot AI does not fetch external URLs; the AI SDK downloads and
|
|
402
|
+
// inlines URL file parts instead. ms:// file references from the Moonshot
|
|
403
|
+
// Files API are passed through natively.
|
|
404
|
+
this.supportedUrls = {
|
|
405
|
+
"image/*": [/^ms:\/\//],
|
|
406
|
+
"video/*": [/^ms:\/\//]
|
|
407
|
+
};
|
|
408
|
+
this.modelId = modelId;
|
|
409
|
+
this.config = config;
|
|
410
|
+
this.failedResponseHandler = createJsonErrorResponseHandler({
|
|
411
|
+
errorSchema: moonshotAIErrorSchema,
|
|
412
|
+
errorToMessage: (error) => error.error.message
|
|
413
|
+
});
|
|
414
|
+
}
|
|
49
415
|
static [WORKFLOW_SERIALIZE](model) {
|
|
50
416
|
return serializeModelOptions({
|
|
51
417
|
modelId: model.modelId,
|
|
@@ -55,54 +421,336 @@ var MoonshotAIChatLanguageModel = class _MoonshotAIChatLanguageModel extends Ope
|
|
|
55
421
|
static [WORKFLOW_DESERIALIZE](options) {
|
|
56
422
|
return new _MoonshotAIChatLanguageModel(options.modelId, options.config);
|
|
57
423
|
}
|
|
58
|
-
|
|
59
|
-
|
|
424
|
+
get provider() {
|
|
425
|
+
return this.config.provider;
|
|
426
|
+
}
|
|
427
|
+
get providerOptionsName() {
|
|
428
|
+
return this.config.provider.split(".")[0].trim();
|
|
429
|
+
}
|
|
430
|
+
async getArgs({
|
|
431
|
+
prompt,
|
|
432
|
+
maxOutputTokens,
|
|
433
|
+
temperature,
|
|
434
|
+
topP,
|
|
435
|
+
topK,
|
|
436
|
+
frequencyPenalty,
|
|
437
|
+
presencePenalty,
|
|
438
|
+
reasoning,
|
|
439
|
+
providerOptions,
|
|
440
|
+
stopSequences,
|
|
441
|
+
responseFormat,
|
|
442
|
+
seed,
|
|
443
|
+
toolChoice,
|
|
444
|
+
tools
|
|
445
|
+
}) {
|
|
446
|
+
var _a, _b, _c;
|
|
447
|
+
const moonshotOptions = (_a = await parseProviderOptions({
|
|
448
|
+
provider: this.providerOptionsName,
|
|
449
|
+
providerOptions,
|
|
450
|
+
schema: moonshotaiLanguageModelOptions
|
|
451
|
+
})) != null ? _a : {};
|
|
452
|
+
const messages = convertToMoonshotAIChatMessages(prompt);
|
|
453
|
+
const allWarnings = [];
|
|
454
|
+
if (topK != null) {
|
|
455
|
+
allWarnings.push({ type: "unsupported", feature: "topK" });
|
|
456
|
+
}
|
|
457
|
+
if (seed != null) {
|
|
458
|
+
allWarnings.push({ type: "unsupported", feature: "seed" });
|
|
459
|
+
}
|
|
460
|
+
const {
|
|
461
|
+
tools: moonshotTools,
|
|
462
|
+
toolChoice: moonshotToolChoice,
|
|
463
|
+
toolWarnings
|
|
464
|
+
} = prepareTools({ tools, toolChoice });
|
|
465
|
+
const thinking = moonshotOptions.thinking;
|
|
466
|
+
let keep;
|
|
467
|
+
if (moonshotOptions.reasoningHistory === "preserved") {
|
|
468
|
+
if (getModelThinkingKeepSupport(this.modelId)) {
|
|
469
|
+
keep = "all";
|
|
470
|
+
} else {
|
|
471
|
+
allWarnings.push({
|
|
472
|
+
type: "unsupported",
|
|
473
|
+
feature: `reasoningHistory 'preserved' is not supported by model "${this.modelId}"`
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
if (reasoning === "none") {
|
|
478
|
+
allWarnings.push({
|
|
479
|
+
type: "unsupported",
|
|
480
|
+
feature: 'reasoning "none" (use providerOptions.moonshotai.thinking to control thinking)'
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
const reasoningEffort = (_b = moonshotOptions.reasoningEffort) != null ? _b : isCustomReasoning(reasoning) && reasoning !== "none" ? mapReasoningToProviderEffort({
|
|
484
|
+
reasoning,
|
|
485
|
+
effortMap: {
|
|
486
|
+
minimal: "low",
|
|
487
|
+
low: "low",
|
|
488
|
+
medium: "high",
|
|
489
|
+
high: "high",
|
|
490
|
+
xhigh: "max"
|
|
491
|
+
},
|
|
492
|
+
warnings: allWarnings
|
|
493
|
+
}) : void 0;
|
|
494
|
+
let response_format;
|
|
495
|
+
if ((responseFormat == null ? void 0 : responseFormat.type) === "json") {
|
|
496
|
+
if (this.config.supportsStructuredOutputs === true && responseFormat.schema != null) {
|
|
497
|
+
const { $schema: _$schema, ...schemaWithoutDollarSchema } = responseFormat.schema;
|
|
498
|
+
response_format = {
|
|
499
|
+
type: "json_schema",
|
|
500
|
+
json_schema: {
|
|
501
|
+
name: (_c = responseFormat.name) != null ? _c : "response",
|
|
502
|
+
schema: schemaWithoutDollarSchema,
|
|
503
|
+
...responseFormat.description != null && {
|
|
504
|
+
description: responseFormat.description
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
} else {
|
|
509
|
+
response_format = { type: "json_object" };
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
return {
|
|
513
|
+
args: {
|
|
514
|
+
model: this.modelId,
|
|
515
|
+
max_tokens: maxOutputTokens,
|
|
516
|
+
temperature,
|
|
517
|
+
top_p: topP,
|
|
518
|
+
frequency_penalty: frequencyPenalty,
|
|
519
|
+
presence_penalty: presencePenalty,
|
|
520
|
+
response_format,
|
|
521
|
+
stop: stopSequences,
|
|
522
|
+
messages,
|
|
523
|
+
tools: moonshotTools,
|
|
524
|
+
tool_choice: moonshotToolChoice,
|
|
525
|
+
...thinking != null || keep != null ? {
|
|
526
|
+
thinking: {
|
|
527
|
+
...(thinking == null ? void 0 : thinking.type) != null && { type: thinking.type },
|
|
528
|
+
...(thinking == null ? void 0 : thinking.budgetTokens) !== void 0 && {
|
|
529
|
+
budget_tokens: thinking.budgetTokens
|
|
530
|
+
},
|
|
531
|
+
...keep != null && { keep }
|
|
532
|
+
}
|
|
533
|
+
} : {},
|
|
534
|
+
...reasoningEffort != null && {
|
|
535
|
+
reasoning_effort: reasoningEffort
|
|
536
|
+
},
|
|
537
|
+
...moonshotOptions.promptCacheKey != null && {
|
|
538
|
+
prompt_cache_key: moonshotOptions.promptCacheKey
|
|
539
|
+
},
|
|
540
|
+
...moonshotOptions.safetyIdentifier != null && {
|
|
541
|
+
safety_identifier: moonshotOptions.safetyIdentifier
|
|
542
|
+
}
|
|
543
|
+
},
|
|
544
|
+
warnings: [...allWarnings, ...toolWarnings]
|
|
545
|
+
};
|
|
60
546
|
}
|
|
61
547
|
async doGenerate(options) {
|
|
62
|
-
var _a, _b;
|
|
63
|
-
const
|
|
64
|
-
const
|
|
548
|
+
var _a, _b, _c, _d, _e;
|
|
549
|
+
const { args, warnings } = await this.getArgs({ ...options });
|
|
550
|
+
const {
|
|
551
|
+
responseHeaders,
|
|
552
|
+
value: responseBody,
|
|
553
|
+
rawValue: rawResponse
|
|
554
|
+
} = await postJsonToApi({
|
|
555
|
+
url: this.config.url({
|
|
556
|
+
path: "/chat/completions",
|
|
557
|
+
modelId: this.modelId
|
|
558
|
+
}),
|
|
559
|
+
headers: combineHeaders((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
|
|
560
|
+
body: args,
|
|
561
|
+
failedResponseHandler: this.failedResponseHandler,
|
|
562
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
563
|
+
moonshotAIChatResponseSchema
|
|
564
|
+
),
|
|
565
|
+
abortSignal: options.abortSignal,
|
|
566
|
+
fetch: this.config.fetch
|
|
567
|
+
});
|
|
568
|
+
const choice = responseBody.choices[0];
|
|
569
|
+
const content = [];
|
|
570
|
+
const reasoning = choice.message.reasoning_content;
|
|
571
|
+
if (reasoning != null && reasoning.length > 0) {
|
|
572
|
+
content.push({ type: "reasoning", text: reasoning });
|
|
573
|
+
}
|
|
574
|
+
if (choice.message.tool_calls != null) {
|
|
575
|
+
for (const toolCall of choice.message.tool_calls) {
|
|
576
|
+
content.push({
|
|
577
|
+
type: "tool-call",
|
|
578
|
+
toolCallId: (_c = toolCall.id) != null ? _c : generateId(),
|
|
579
|
+
toolName: toolCall.function.name,
|
|
580
|
+
input: (_d = toolCall.function.arguments) != null ? _d : ""
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
const text = choice.message.content;
|
|
585
|
+
if (text != null && text.length > 0) {
|
|
586
|
+
content.push({ type: "text", text });
|
|
587
|
+
}
|
|
65
588
|
return {
|
|
66
|
-
|
|
67
|
-
|
|
589
|
+
content,
|
|
590
|
+
finishReason: {
|
|
591
|
+
unified: mapMoonshotAIFinishReason(choice.finish_reason),
|
|
592
|
+
raw: (_e = choice.finish_reason) != null ? _e : void 0
|
|
593
|
+
},
|
|
594
|
+
usage: convertMoonshotAIChatUsage(responseBody.usage),
|
|
595
|
+
request: { body: args },
|
|
596
|
+
response: {
|
|
597
|
+
...getResponseMetadata(responseBody),
|
|
598
|
+
headers: responseHeaders,
|
|
599
|
+
body: rawResponse
|
|
600
|
+
},
|
|
601
|
+
warnings
|
|
68
602
|
};
|
|
69
603
|
}
|
|
70
604
|
async doStream(options) {
|
|
71
|
-
|
|
605
|
+
var _a, _b;
|
|
606
|
+
const { args, warnings } = await this.getArgs({ ...options });
|
|
607
|
+
const body = {
|
|
608
|
+
...args,
|
|
609
|
+
stream: true,
|
|
610
|
+
...this.config.includeUsage && {
|
|
611
|
+
stream_options: { include_usage: true }
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
615
|
+
url: this.config.url({
|
|
616
|
+
path: "/chat/completions",
|
|
617
|
+
modelId: this.modelId
|
|
618
|
+
}),
|
|
619
|
+
headers: combineHeaders((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
|
|
620
|
+
body,
|
|
621
|
+
failedResponseHandler: this.failedResponseHandler,
|
|
622
|
+
successfulResponseHandler: createEventSourceResponseHandler(
|
|
623
|
+
moonshotAIChatChunkSchema
|
|
624
|
+
),
|
|
625
|
+
abortSignal: options.abortSignal,
|
|
626
|
+
fetch: this.config.fetch
|
|
627
|
+
});
|
|
628
|
+
let toolCallTracker;
|
|
629
|
+
let finishReason = {
|
|
630
|
+
unified: "other",
|
|
631
|
+
raw: void 0
|
|
632
|
+
};
|
|
633
|
+
let usage = void 0;
|
|
634
|
+
let isFirstChunk = true;
|
|
635
|
+
let isActiveReasoning = false;
|
|
636
|
+
let isActiveText = false;
|
|
72
637
|
return {
|
|
73
|
-
|
|
74
|
-
stream: result.stream.pipeThrough(
|
|
638
|
+
stream: response.pipeThrough(
|
|
75
639
|
new TransformStream({
|
|
640
|
+
start(controller) {
|
|
641
|
+
toolCallTracker = new StreamingToolCallTracker(controller, {
|
|
642
|
+
generateId
|
|
643
|
+
});
|
|
644
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
645
|
+
},
|
|
76
646
|
transform(chunk, controller) {
|
|
77
|
-
if (
|
|
647
|
+
if (options.includeRawChunks) {
|
|
648
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
649
|
+
}
|
|
650
|
+
if (!chunk.success) {
|
|
651
|
+
finishReason = { unified: "error", raw: void 0 };
|
|
652
|
+
controller.enqueue({ type: "error", error: chunk.error });
|
|
653
|
+
return;
|
|
654
|
+
}
|
|
655
|
+
const value = chunk.value;
|
|
656
|
+
if ("error" in value) {
|
|
657
|
+
finishReason = { unified: "error", raw: void 0 };
|
|
658
|
+
controller.enqueue({ type: "error", error: value.error.message });
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
if (isFirstChunk) {
|
|
662
|
+
isFirstChunk = false;
|
|
663
|
+
controller.enqueue({
|
|
664
|
+
type: "response-metadata",
|
|
665
|
+
...getResponseMetadata(value)
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
if (value.usage != null) {
|
|
669
|
+
usage = value.usage;
|
|
670
|
+
}
|
|
671
|
+
const choice = value.choices[0];
|
|
672
|
+
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
673
|
+
finishReason = {
|
|
674
|
+
unified: mapMoonshotAIFinishReason(choice.finish_reason),
|
|
675
|
+
raw: choice.finish_reason
|
|
676
|
+
};
|
|
677
|
+
}
|
|
678
|
+
if ((choice == null ? void 0 : choice.delta) == null) {
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
const delta = choice.delta;
|
|
682
|
+
const reasoningContent = delta.reasoning_content;
|
|
683
|
+
if (reasoningContent) {
|
|
684
|
+
if (!isActiveReasoning) {
|
|
685
|
+
controller.enqueue({
|
|
686
|
+
type: "reasoning-start",
|
|
687
|
+
id: "reasoning-0"
|
|
688
|
+
});
|
|
689
|
+
isActiveReasoning = true;
|
|
690
|
+
}
|
|
691
|
+
controller.enqueue({
|
|
692
|
+
type: "reasoning-delta",
|
|
693
|
+
id: "reasoning-0",
|
|
694
|
+
delta: reasoningContent
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
if (delta.content) {
|
|
698
|
+
if (!isActiveText) {
|
|
699
|
+
controller.enqueue({ type: "text-start", id: "txt-0" });
|
|
700
|
+
isActiveText = true;
|
|
701
|
+
}
|
|
702
|
+
if (isActiveReasoning) {
|
|
703
|
+
controller.enqueue({
|
|
704
|
+
type: "reasoning-end",
|
|
705
|
+
id: "reasoning-0"
|
|
706
|
+
});
|
|
707
|
+
isActiveReasoning = false;
|
|
708
|
+
}
|
|
78
709
|
controller.enqueue({
|
|
79
|
-
|
|
80
|
-
|
|
710
|
+
type: "text-delta",
|
|
711
|
+
id: "txt-0",
|
|
712
|
+
delta: delta.content
|
|
81
713
|
});
|
|
82
|
-
} else {
|
|
83
|
-
controller.enqueue(chunk);
|
|
84
714
|
}
|
|
715
|
+
if (delta.tool_calls != null) {
|
|
716
|
+
if (isActiveReasoning) {
|
|
717
|
+
controller.enqueue({
|
|
718
|
+
type: "reasoning-end",
|
|
719
|
+
id: "reasoning-0"
|
|
720
|
+
});
|
|
721
|
+
isActiveReasoning = false;
|
|
722
|
+
}
|
|
723
|
+
for (const toolCallDelta of delta.tool_calls) {
|
|
724
|
+
toolCallTracker.processDelta(toolCallDelta);
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
},
|
|
728
|
+
flush(controller) {
|
|
729
|
+
if (isActiveReasoning) {
|
|
730
|
+
controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
|
|
731
|
+
}
|
|
732
|
+
if (isActiveText) {
|
|
733
|
+
controller.enqueue({ type: "text-end", id: "txt-0" });
|
|
734
|
+
}
|
|
735
|
+
toolCallTracker.flush();
|
|
736
|
+
controller.enqueue({
|
|
737
|
+
type: "finish",
|
|
738
|
+
finishReason,
|
|
739
|
+
usage: convertMoonshotAIChatUsage(usage)
|
|
740
|
+
});
|
|
85
741
|
}
|
|
86
742
|
})
|
|
87
|
-
)
|
|
743
|
+
),
|
|
744
|
+
request: { body },
|
|
745
|
+
response: { headers: responseHeaders }
|
|
88
746
|
};
|
|
89
747
|
}
|
|
90
748
|
};
|
|
91
749
|
|
|
92
750
|
// src/version.ts
|
|
93
|
-
var VERSION = true ? "3.0.
|
|
751
|
+
var VERSION = true ? "3.0.32" : "0.0.0-test";
|
|
94
752
|
|
|
95
753
|
// src/moonshotai-provider.ts
|
|
96
|
-
var moonshotaiErrorSchema = z.object({
|
|
97
|
-
error: z.object({
|
|
98
|
-
message: z.string(),
|
|
99
|
-
type: z.string().nullish()
|
|
100
|
-
})
|
|
101
|
-
});
|
|
102
|
-
var moonshotaiErrorStructure = {
|
|
103
|
-
errorSchema: moonshotaiErrorSchema,
|
|
104
|
-
errorToMessage: (data) => data.error.message
|
|
105
|
-
};
|
|
106
754
|
var defaultBaseURL = "https://api.moonshot.ai/v1";
|
|
107
755
|
function getModelStructuredOutputSupport(modelId) {
|
|
108
756
|
if (modelId.startsWith("kimi-k")) return true;
|
|
@@ -122,49 +770,14 @@ function createMoonshotAI(options = {}) {
|
|
|
122
770
|
},
|
|
123
771
|
`ai-sdk/moonshotai/${VERSION}`
|
|
124
772
|
);
|
|
125
|
-
const getCommonModelConfig = (modelType) => ({
|
|
126
|
-
provider: `moonshotai.${modelType}`,
|
|
127
|
-
url: ({ path }) => `${baseURL}${path}`,
|
|
128
|
-
headers: getHeaders,
|
|
129
|
-
fetch: options.fetch
|
|
130
|
-
});
|
|
131
773
|
const createChatModel = (modelId) => {
|
|
132
774
|
return new MoonshotAIChatLanguageModel(modelId, {
|
|
133
|
-
|
|
775
|
+
provider: "moonshotai.chat",
|
|
776
|
+
url: ({ path }) => `${baseURL}${path}`,
|
|
777
|
+
headers: getHeaders,
|
|
778
|
+
fetch: options.fetch,
|
|
134
779
|
includeUsage: true,
|
|
135
|
-
|
|
136
|
-
supportsStructuredOutputs: getModelStructuredOutputSupport(modelId),
|
|
137
|
-
transformRequestBody: (args) => {
|
|
138
|
-
var _a2, _b;
|
|
139
|
-
const thinking = args.thinking;
|
|
140
|
-
const reasoningHistory = args.reasoningHistory;
|
|
141
|
-
const { thinking: _, reasoningHistory: __, ...rest } = args;
|
|
142
|
-
const schema = (_b = (_a2 = rest.response_format) == null ? void 0 : _a2.json_schema) == null ? void 0 : _b.schema;
|
|
143
|
-
if (schema != null) {
|
|
144
|
-
const { $schema: _$schema, ...schemaWithoutDollarSchema } = schema;
|
|
145
|
-
rest.response_format = {
|
|
146
|
-
...rest.response_format,
|
|
147
|
-
json_schema: {
|
|
148
|
-
...rest.response_format.json_schema,
|
|
149
|
-
schema: schemaWithoutDollarSchema
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
return {
|
|
154
|
-
...rest,
|
|
155
|
-
...thinking && {
|
|
156
|
-
thinking: {
|
|
157
|
-
type: thinking.type,
|
|
158
|
-
...thinking.budgetTokens !== void 0 && {
|
|
159
|
-
budget_tokens: thinking.budgetTokens
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
},
|
|
163
|
-
...reasoningHistory && {
|
|
164
|
-
reasoning_history: reasoningHistory
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
}
|
|
780
|
+
supportsStructuredOutputs: getModelStructuredOutputSupport(modelId)
|
|
168
781
|
});
|
|
169
782
|
};
|
|
170
783
|
const provider = (modelId) => createChatModel(modelId);
|