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