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