@ai-sdk/moonshotai 0.0.20 → 0.0.24
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 +43 -0
- package/README.md +40 -12
- package/dist/index.d.mts +62 -18
- package/dist/index.d.ts +62 -18
- package/dist/index.js +767 -20
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +762 -13
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +3 -0
- package/src/moonshotai-chat-language-model.ts +607 -7
- package/src/moonshotai-chat-metadata-extractor.ts +112 -0
- package/src/moonshotai-chat-options.ts +187 -9
- package/src/moonshotai-provider.ts +17 -0
- package/src/normalize-json-schema-for-mfjs.ts +103 -0
package/dist/index.mjs
CHANGED
|
@@ -7,10 +7,15 @@ import {
|
|
|
7
7
|
withoutTrailingSlash,
|
|
8
8
|
withUserAgentSuffix
|
|
9
9
|
} from "@ai-sdk/provider-utils";
|
|
10
|
-
import { z } from "zod/v4";
|
|
10
|
+
import { z as z3 } from "zod/v4";
|
|
11
11
|
|
|
12
12
|
// src/moonshotai-chat-language-model.ts
|
|
13
13
|
import { OpenAICompatibleChatLanguageModel } from "@ai-sdk/openai-compatible";
|
|
14
|
+
import {
|
|
15
|
+
InvalidPromptError
|
|
16
|
+
} from "@ai-sdk/provider";
|
|
17
|
+
import { parseProviderOptions } from "@ai-sdk/provider-utils";
|
|
18
|
+
import { z as z2 } from "zod/v4";
|
|
14
19
|
|
|
15
20
|
// src/convert-moonshotai-chat-usage.ts
|
|
16
21
|
function convertMoonshotAIChatUsage(usage) {
|
|
@@ -33,37 +38,767 @@ function convertMoonshotAIChatUsage(usage) {
|
|
|
33
38
|
};
|
|
34
39
|
}
|
|
35
40
|
|
|
41
|
+
// src/moonshotai-chat-metadata-extractor.ts
|
|
42
|
+
function isRecord(value) {
|
|
43
|
+
return value != null && typeof value === "object" && !Array.isArray(value);
|
|
44
|
+
}
|
|
45
|
+
function getFirstChoice(value) {
|
|
46
|
+
const choices = value.choices;
|
|
47
|
+
return Array.isArray(choices) && isRecord(choices[0]) ? choices[0] : void 0;
|
|
48
|
+
}
|
|
49
|
+
function getToolCallTypes(value) {
|
|
50
|
+
if (!Array.isArray(value)) {
|
|
51
|
+
return void 0;
|
|
52
|
+
}
|
|
53
|
+
return value.flatMap(
|
|
54
|
+
(toolCall) => isRecord(toolCall) && toolCall.type === "function" ? [toolCall.type] : []
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
function extractMetadata(value) {
|
|
58
|
+
if (!isRecord(value)) {
|
|
59
|
+
return {};
|
|
60
|
+
}
|
|
61
|
+
const choice = getFirstChoice(value);
|
|
62
|
+
const message = choice != null && isRecord(choice.message) ? choice.message : void 0;
|
|
63
|
+
const toolCallTypes = getToolCallTypes(message == null ? void 0 : message.tool_calls);
|
|
64
|
+
return {
|
|
65
|
+
...value.object === "chat.completion" && {
|
|
66
|
+
responseObject: value.object
|
|
67
|
+
},
|
|
68
|
+
...typeof (choice == null ? void 0 : choice.index) === "number" && {
|
|
69
|
+
choiceIndex: choice.index
|
|
70
|
+
},
|
|
71
|
+
...(message == null ? void 0 : message.role) === "assistant" && {
|
|
72
|
+
messageRole: message.role
|
|
73
|
+
},
|
|
74
|
+
...toolCallTypes != null && { toolCallTypes }
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
var moonshotAIChatMetadataExtractor = {
|
|
78
|
+
async extractMetadata({ parsedBody }) {
|
|
79
|
+
return { moonshotai: extractMetadata(parsedBody) };
|
|
80
|
+
},
|
|
81
|
+
createStreamExtractor() {
|
|
82
|
+
let responseObject;
|
|
83
|
+
let choiceIndex;
|
|
84
|
+
let messageRole;
|
|
85
|
+
const toolCallTypes = /* @__PURE__ */ new Map();
|
|
86
|
+
return {
|
|
87
|
+
processChunk(parsedChunk) {
|
|
88
|
+
if (!isRecord(parsedChunk)) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (parsedChunk.object === "chat.completion.chunk") {
|
|
92
|
+
responseObject = parsedChunk.object;
|
|
93
|
+
}
|
|
94
|
+
const choice = getFirstChoice(parsedChunk);
|
|
95
|
+
if (typeof (choice == null ? void 0 : choice.index) === "number") {
|
|
96
|
+
choiceIndex = choice.index;
|
|
97
|
+
}
|
|
98
|
+
const delta = choice != null && isRecord(choice.delta) ? choice.delta : void 0;
|
|
99
|
+
if ((delta == null ? void 0 : delta.role) === "assistant") {
|
|
100
|
+
messageRole = delta.role;
|
|
101
|
+
}
|
|
102
|
+
if (Array.isArray(delta == null ? void 0 : delta.tool_calls)) {
|
|
103
|
+
for (const [fallbackIndex, toolCall] of delta.tool_calls.entries()) {
|
|
104
|
+
if (!isRecord(toolCall) || toolCall.type !== "function") {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const index = typeof toolCall.index === "number" ? toolCall.index : fallbackIndex;
|
|
108
|
+
toolCallTypes.set(index, toolCall.type);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
buildMetadata() {
|
|
113
|
+
return {
|
|
114
|
+
moonshotai: {
|
|
115
|
+
...responseObject != null && { responseObject },
|
|
116
|
+
...choiceIndex != null && { choiceIndex },
|
|
117
|
+
...messageRole != null && { messageRole },
|
|
118
|
+
...toolCallTypes.size > 0 && {
|
|
119
|
+
toolCallTypes: [...toolCallTypes.entries()].sort(([left], [right]) => left - right).map(([, type]) => type)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// src/moonshotai-chat-options.ts
|
|
129
|
+
import { z } from "zod/v4";
|
|
130
|
+
function getMoonshotAIModelFamily(modelId) {
|
|
131
|
+
if (modelId === "kimi-k2.5") return "kimi-k2.5";
|
|
132
|
+
if (modelId === "kimi-k2.6") return "kimi-k2.6";
|
|
133
|
+
if (modelId === "kimi-k2.7-code" || modelId === "kimi-k2.7-code-highspeed") {
|
|
134
|
+
return "kimi-k2.7";
|
|
135
|
+
}
|
|
136
|
+
if (modelId === "kimi-k3") return "kimi-k3";
|
|
137
|
+
if (modelId.startsWith("moonshot-v1-")) return "moonshot-v1";
|
|
138
|
+
return "unknown";
|
|
139
|
+
}
|
|
140
|
+
function isMoonshotAIKimiModel(modelId) {
|
|
141
|
+
return getMoonshotAIModelFamily(modelId).startsWith("kimi-");
|
|
142
|
+
}
|
|
143
|
+
var moonshotaiProviderOptions = z.object({
|
|
144
|
+
/**
|
|
145
|
+
* Whether to use strict JSON schema validation for structured outputs.
|
|
146
|
+
*
|
|
147
|
+
* @default true
|
|
148
|
+
*/
|
|
149
|
+
strictJsonSchema: z.boolean().optional(),
|
|
150
|
+
/**
|
|
151
|
+
* Whether to return log probabilities for generated tokens.
|
|
152
|
+
*/
|
|
153
|
+
logprobs: z.boolean().optional(),
|
|
154
|
+
/**
|
|
155
|
+
* Number of most likely tokens to return at each token position.
|
|
156
|
+
*
|
|
157
|
+
* Setting this option automatically enables `logprobs`.
|
|
158
|
+
*/
|
|
159
|
+
topLogprobs: z.number().int().min(0).max(20).optional(),
|
|
160
|
+
/**
|
|
161
|
+
* Reasoning effort for Kimi K3. Supports `low`, `high`, and `max`;
|
|
162
|
+
* defaults to `max`.
|
|
163
|
+
*/
|
|
164
|
+
reasoningEffort: z.enum(["low", "high", "max"]).optional(),
|
|
165
|
+
/**
|
|
166
|
+
* Static predicted content that can accelerate responses when much of the
|
|
167
|
+
* output is known ahead of time.
|
|
168
|
+
*/
|
|
169
|
+
prediction: z.object({
|
|
170
|
+
type: z.literal("content"),
|
|
171
|
+
content: z.union([
|
|
172
|
+
z.string(),
|
|
173
|
+
z.array(z.object({ type: z.literal("text"), text: z.string() }))
|
|
174
|
+
])
|
|
175
|
+
}).optional(),
|
|
176
|
+
/**
|
|
177
|
+
* Thinking configuration for Kimi K2.x models. Kimi K2.5 and K2.6 support
|
|
178
|
+
* enabling or disabling thinking. Kimi K2.7 Code always has thinking
|
|
179
|
+
* enabled.
|
|
180
|
+
*/
|
|
181
|
+
thinking: z.object({
|
|
182
|
+
type: z.enum(["enabled", "disabled"]).optional(),
|
|
183
|
+
/**
|
|
184
|
+
* @deprecated Moonshot Chat Completions does not support thinking
|
|
185
|
+
* budgets. Accepted for backwards compatibility, then omitted with a
|
|
186
|
+
* warning.
|
|
187
|
+
*/
|
|
188
|
+
budgetTokens: z.number().int().min(1024).optional()
|
|
189
|
+
}).optional(),
|
|
190
|
+
/**
|
|
191
|
+
* Controls preserved reasoning behavior in multi-turn conversations.
|
|
192
|
+
* `disabled` and `interleaved` are compatibility values that leave the
|
|
193
|
+
* request unchanged. `preserved` maps to `thinking.keep: 'all'` for Kimi
|
|
194
|
+
* K2.6. Kimi K2.7 and K3 preserve reasoning by default.
|
|
195
|
+
*/
|
|
196
|
+
reasoningHistory: z.enum(["disabled", "interleaved", "preserved"]).optional()
|
|
197
|
+
});
|
|
198
|
+
var moonshotaiMessageProviderOptions = z.object({
|
|
199
|
+
/**
|
|
200
|
+
* The name of the participant represented by the message.
|
|
201
|
+
*
|
|
202
|
+
* Supported on system, user, and assistant messages.
|
|
203
|
+
*/
|
|
204
|
+
name: z.string().optional()
|
|
205
|
+
});
|
|
206
|
+
var moonshotaiAssistantMessageProviderOptions = moonshotaiMessageProviderOptions.extend({
|
|
207
|
+
/**
|
|
208
|
+
* Whether the assistant message content is a partial response that Moonshot
|
|
209
|
+
* should continue. Only supported on the final assistant message and cannot
|
|
210
|
+
* be combined with JSON object response format.
|
|
211
|
+
*/
|
|
212
|
+
partial: z.literal(true).optional()
|
|
213
|
+
});
|
|
214
|
+
var moonshotaiDynamicToolSchema = z.object({
|
|
215
|
+
type: z.literal("function"),
|
|
216
|
+
name: z.string(),
|
|
217
|
+
description: z.string().optional(),
|
|
218
|
+
inputSchema: z.record(z.string(), z.unknown()),
|
|
219
|
+
strict: z.boolean().optional()
|
|
220
|
+
});
|
|
221
|
+
var moonshotaiAllMessageProviderOptions = moonshotaiAssistantMessageProviderOptions.extend({
|
|
222
|
+
/** Function tools to load at this point in a Kimi K3 conversation. */
|
|
223
|
+
tools: z.array(moonshotaiDynamicToolSchema).optional()
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
// src/normalize-json-schema-for-mfjs.ts
|
|
227
|
+
import { UnsupportedFunctionalityError } from "@ai-sdk/provider";
|
|
228
|
+
var SCHEMA_ARRAY_KEYS = ["allOf", "anyOf", "oneOf", "prefixItems"];
|
|
229
|
+
var SCHEMA_MAP_KEYS = [
|
|
230
|
+
"properties",
|
|
231
|
+
"patternProperties",
|
|
232
|
+
"$defs",
|
|
233
|
+
"dependentSchemas"
|
|
234
|
+
];
|
|
235
|
+
var SCHEMA_SINGLE_KEYS = [
|
|
236
|
+
"additionalProperties",
|
|
237
|
+
"propertyNames",
|
|
238
|
+
"items",
|
|
239
|
+
"contains",
|
|
240
|
+
"not",
|
|
241
|
+
"if",
|
|
242
|
+
"then",
|
|
243
|
+
"else"
|
|
244
|
+
];
|
|
245
|
+
function isRecord2(value) {
|
|
246
|
+
return value != null && typeof value === "object" && !Array.isArray(value);
|
|
247
|
+
}
|
|
248
|
+
function normalizeJsonSchemaForMFJS(schema) {
|
|
249
|
+
return normalizeDefinition(schema, true);
|
|
250
|
+
}
|
|
251
|
+
function normalizeDefinition(definition, isRoot) {
|
|
252
|
+
if (typeof definition === "boolean" || !isRecord2(definition)) {
|
|
253
|
+
if (isRoot) {
|
|
254
|
+
throw new UnsupportedFunctionalityError({
|
|
255
|
+
functionality: 'tool parameters must be a JSON Schema object with type "object" for moonshotai (MFJS)'
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
return definition;
|
|
259
|
+
}
|
|
260
|
+
if (isRoot && definition.type !== "object") {
|
|
261
|
+
throw new UnsupportedFunctionalityError({
|
|
262
|
+
functionality: 'tool parameters must be a JSON Schema object with type "object" for moonshotai (MFJS)'
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
const result = { ...definition };
|
|
266
|
+
if (Array.isArray(result.items)) {
|
|
267
|
+
const tuple = result.items;
|
|
268
|
+
result.prefixItems = [
|
|
269
|
+
...Array.isArray(result.prefixItems) ? result.prefixItems : [],
|
|
270
|
+
...tuple.map((item) => normalizeDefinition(item, false))
|
|
271
|
+
];
|
|
272
|
+
delete result.items;
|
|
273
|
+
} else if (isRecord2(result.items)) {
|
|
274
|
+
result.items = normalizeDefinition(result.items, false);
|
|
275
|
+
}
|
|
276
|
+
if (typeof result.type === "string" && Array.isArray(result.anyOf)) {
|
|
277
|
+
const parentType = result.type;
|
|
278
|
+
delete result.type;
|
|
279
|
+
result.anyOf = result.anyOf.map(
|
|
280
|
+
(branch) => isRecord2(branch) && branch.type == null ? { type: parentType, ...branch } : branch
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
for (const key of SCHEMA_ARRAY_KEYS) {
|
|
284
|
+
const value = result[key];
|
|
285
|
+
if (Array.isArray(value)) {
|
|
286
|
+
result[key] = value.map((item) => normalizeDefinition(item, false));
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
for (const key of SCHEMA_MAP_KEYS) {
|
|
290
|
+
const value = result[key];
|
|
291
|
+
if (isRecord2(value)) {
|
|
292
|
+
result[key] = Object.fromEntries(
|
|
293
|
+
Object.entries(value).map(([key2, item]) => [
|
|
294
|
+
key2,
|
|
295
|
+
normalizeDefinition(item, false)
|
|
296
|
+
])
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
for (const key of SCHEMA_SINGLE_KEYS) {
|
|
301
|
+
const value = result[key];
|
|
302
|
+
if (isRecord2(value) || typeof value === "boolean") {
|
|
303
|
+
result[key] = normalizeDefinition(value, false);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return result;
|
|
307
|
+
}
|
|
308
|
+
|
|
36
309
|
// src/moonshotai-chat-language-model.ts
|
|
310
|
+
function transformMoonshotRequestBody(args) {
|
|
311
|
+
var _a, _b, _c;
|
|
312
|
+
const {
|
|
313
|
+
strictJsonSchema,
|
|
314
|
+
max_tokens: maxTokens,
|
|
315
|
+
logprobs,
|
|
316
|
+
topLogprobs,
|
|
317
|
+
...transformedArgs
|
|
318
|
+
} = args;
|
|
319
|
+
const moonshotArgs = {
|
|
320
|
+
...transformedArgs,
|
|
321
|
+
...(logprobs === true || topLogprobs != null) && { logprobs: true },
|
|
322
|
+
...topLogprobs != null && { top_logprobs: topLogprobs },
|
|
323
|
+
...maxTokens != null ? { max_completion_tokens: maxTokens } : {}
|
|
324
|
+
};
|
|
325
|
+
if (Array.isArray(moonshotArgs.messages)) {
|
|
326
|
+
moonshotArgs.messages = moonshotArgs.messages.map(
|
|
327
|
+
(message) => {
|
|
328
|
+
if (message.role !== "system" || !Array.isArray(message.tools)) {
|
|
329
|
+
return message;
|
|
330
|
+
}
|
|
331
|
+
const { content: _content, ...dynamicToolMessage } = message;
|
|
332
|
+
return dynamicToolMessage;
|
|
333
|
+
}
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
const responseFormat = moonshotArgs.response_format;
|
|
337
|
+
if ((responseFormat == null ? void 0 : responseFormat.type) === "json_object" && ((_a = moonshotArgs.messages) == null ? void 0 : _a.some(
|
|
338
|
+
(message) => message.partial === true
|
|
339
|
+
))) {
|
|
340
|
+
throw new InvalidPromptError({
|
|
341
|
+
prompt: args.messages,
|
|
342
|
+
message: "Moonshot AI Partial Mode cannot be combined with JSON object response format."
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
if ((responseFormat == null ? void 0 : responseFormat.type) !== "json_schema" || ((_b = responseFormat.json_schema) == null ? void 0 : _b.schema) == null) {
|
|
346
|
+
return moonshotArgs;
|
|
347
|
+
}
|
|
348
|
+
const { $schema: _$schema, ...schemaWithoutDollarSchema } = responseFormat.json_schema.schema;
|
|
349
|
+
return {
|
|
350
|
+
...moonshotArgs,
|
|
351
|
+
response_format: {
|
|
352
|
+
type: "json_schema",
|
|
353
|
+
json_schema: {
|
|
354
|
+
name: (_c = responseFormat.json_schema.name) != null ? _c : "response",
|
|
355
|
+
strict: strictJsonSchema != null ? strictJsonSchema : true,
|
|
356
|
+
schema: normalizeJsonSchemaForMFJS(schemaWithoutDollarSchema)
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
var moonshotAIValidatedOptionsSchema = moonshotaiProviderOptions.pick({
|
|
362
|
+
logprobs: true,
|
|
363
|
+
topLogprobs: true,
|
|
364
|
+
prediction: true
|
|
365
|
+
});
|
|
366
|
+
async function validateProviderOptions(options) {
|
|
367
|
+
await parseProviderOptions({
|
|
368
|
+
provider: "moonshotai",
|
|
369
|
+
providerOptions: options.providerOptions,
|
|
370
|
+
schema: moonshotAIValidatedOptionsSchema
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
async function prepareMessageOptions({
|
|
374
|
+
modelId,
|
|
375
|
+
options
|
|
376
|
+
}) {
|
|
377
|
+
var _a, _b, _c, _d;
|
|
378
|
+
const prompt = [];
|
|
379
|
+
const warnings = [];
|
|
380
|
+
for (const [index, message] of options.prompt.entries()) {
|
|
381
|
+
const messageOptions = await parseProviderOptions({
|
|
382
|
+
provider: "moonshotai",
|
|
383
|
+
providerOptions: message.providerOptions,
|
|
384
|
+
schema: moonshotaiAllMessageProviderOptions
|
|
385
|
+
});
|
|
386
|
+
if ((messageOptions == null ? void 0 : messageOptions.partial) === true && message.role !== "assistant") {
|
|
387
|
+
throw new InvalidPromptError({
|
|
388
|
+
prompt: options.prompt,
|
|
389
|
+
message: "Moonshot AI Partial Mode requires `partial: true` on an assistant message."
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
if ((messageOptions == null ? void 0 : messageOptions.tools) != null && message.role !== "system") {
|
|
393
|
+
throw new InvalidPromptError({
|
|
394
|
+
prompt: options.prompt,
|
|
395
|
+
message: "Moonshot dynamic tools must be configured on a system message."
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
if ((messageOptions == null ? void 0 : messageOptions.partial) === true && index !== options.prompt.length - 1) {
|
|
399
|
+
throw new InvalidPromptError({
|
|
400
|
+
prompt: options.prompt,
|
|
401
|
+
message: "Moonshot AI Partial Mode requires the partial assistant message to be the final message."
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
if (((_a = messageOptions == null ? void 0 : messageOptions.tools) == null ? void 0 : _a.length) && message.role === "system") {
|
|
405
|
+
if (message.content.length > 0) {
|
|
406
|
+
throw new InvalidPromptError({
|
|
407
|
+
prompt: options.prompt,
|
|
408
|
+
message: "A Moonshot dynamic-tool system message must use empty content because the API forbids content alongside tools."
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
const modelFamily = getMoonshotAIModelFamily(modelId);
|
|
412
|
+
if (modelFamily !== "kimi-k3" && modelFamily !== "unknown") {
|
|
413
|
+
warnings.push({
|
|
414
|
+
type: "other",
|
|
415
|
+
message: `Moonshot documents dynamic tool loading only for Kimi K3. The dynamic system message has been omitted for model "${modelId}".`
|
|
416
|
+
});
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
419
|
+
prompt.push({
|
|
420
|
+
...message,
|
|
421
|
+
providerOptions: {
|
|
422
|
+
...message.providerOptions,
|
|
423
|
+
openaiCompatible: {
|
|
424
|
+
...(_b = message.providerOptions) == null ? void 0 : _b.openaiCompatible,
|
|
425
|
+
tools: messageOptions.tools.map((tool) => ({
|
|
426
|
+
type: "function",
|
|
427
|
+
function: {
|
|
428
|
+
name: tool.name,
|
|
429
|
+
...tool.description != null ? { description: tool.description } : {},
|
|
430
|
+
parameters: normalizeJsonSchemaForMFJS(
|
|
431
|
+
tool.inputSchema
|
|
432
|
+
),
|
|
433
|
+
...tool.strict != null ? { strict: tool.strict } : {}
|
|
434
|
+
}
|
|
435
|
+
}))
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
if ((messageOptions == null ? void 0 : messageOptions.name) == null && (messageOptions == null ? void 0 : messageOptions.partial) !== true) {
|
|
442
|
+
prompt.push(message);
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
if (message.role === "tool" && (messageOptions == null ? void 0 : messageOptions.name) != null) {
|
|
446
|
+
warnings.push({
|
|
447
|
+
type: "other",
|
|
448
|
+
message: "Moonshot AI does not support message names on tool messages. The name has been omitted."
|
|
449
|
+
});
|
|
450
|
+
prompt.push(message);
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
const openAICompatibleOptions = {
|
|
454
|
+
...(_c = message.providerOptions) == null ? void 0 : _c.openaiCompatible,
|
|
455
|
+
...messageOptions.name != null && { name: messageOptions.name },
|
|
456
|
+
...messageOptions.partial === true && { partial: true }
|
|
457
|
+
};
|
|
458
|
+
if (message.role === "user" && message.content.length === 1 && message.content[0].type === "text") {
|
|
459
|
+
const textPart = message.content[0];
|
|
460
|
+
prompt.push({
|
|
461
|
+
...message,
|
|
462
|
+
content: [
|
|
463
|
+
{
|
|
464
|
+
...textPart,
|
|
465
|
+
providerOptions: {
|
|
466
|
+
...textPart.providerOptions,
|
|
467
|
+
openaiCompatible: {
|
|
468
|
+
...openAICompatibleOptions,
|
|
469
|
+
...(_d = textPart.providerOptions) == null ? void 0 : _d.openaiCompatible,
|
|
470
|
+
...messageOptions.name != null && {
|
|
471
|
+
name: messageOptions.name
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
]
|
|
477
|
+
});
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
prompt.push({
|
|
481
|
+
...message,
|
|
482
|
+
providerOptions: {
|
|
483
|
+
...message.providerOptions,
|
|
484
|
+
openaiCompatible: openAICompatibleOptions
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
return {
|
|
489
|
+
options: { ...options, prompt },
|
|
490
|
+
warnings
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
var moonshotAIChatLogprobSchema = z2.object({
|
|
494
|
+
token: z2.string(),
|
|
495
|
+
logprob: z2.number(),
|
|
496
|
+
bytes: z2.array(z2.number()).nullable(),
|
|
497
|
+
top_logprobs: z2.array(
|
|
498
|
+
z2.object({
|
|
499
|
+
token: z2.string(),
|
|
500
|
+
logprob: z2.number(),
|
|
501
|
+
bytes: z2.array(z2.number()).nullable()
|
|
502
|
+
})
|
|
503
|
+
)
|
|
504
|
+
});
|
|
505
|
+
var moonshotAIChatLogprobsSchema = z2.object({
|
|
506
|
+
content: z2.array(moonshotAIChatLogprobSchema).nullish()
|
|
507
|
+
}).nullish();
|
|
508
|
+
function extractMoonshotAIChatLogprobs(value) {
|
|
509
|
+
var _a, _b, _c;
|
|
510
|
+
const result = moonshotAIChatLogprobsSchema.safeParse(
|
|
511
|
+
(_b = (_a = value == null ? void 0 : value.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.logprobs
|
|
512
|
+
);
|
|
513
|
+
return result.success ? (_c = result.data) != null ? _c : void 0 : void 0;
|
|
514
|
+
}
|
|
515
|
+
function prepareSamplingOptions({
|
|
516
|
+
modelId,
|
|
517
|
+
options
|
|
518
|
+
}) {
|
|
519
|
+
if (!isMoonshotAIKimiModel(modelId)) {
|
|
520
|
+
return { options, warnings: [] };
|
|
521
|
+
}
|
|
522
|
+
const warnings = [];
|
|
523
|
+
const samplingSettings = [
|
|
524
|
+
["temperature", options.temperature],
|
|
525
|
+
["topP", options.topP],
|
|
526
|
+
["frequencyPenalty", options.frequencyPenalty],
|
|
527
|
+
["presencePenalty", options.presencePenalty]
|
|
528
|
+
];
|
|
529
|
+
for (const [setting, value] of samplingSettings) {
|
|
530
|
+
if (value != null) {
|
|
531
|
+
warnings.push({
|
|
532
|
+
type: "unsupported-setting",
|
|
533
|
+
setting,
|
|
534
|
+
details: `${setting} is fixed by model "${modelId}" and has been omitted.`
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
return {
|
|
539
|
+
options: {
|
|
540
|
+
...options,
|
|
541
|
+
temperature: void 0,
|
|
542
|
+
topP: void 0,
|
|
543
|
+
frequencyPenalty: void 0,
|
|
544
|
+
presencePenalty: void 0
|
|
545
|
+
},
|
|
546
|
+
warnings
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
function prepareToolChoiceOptions({
|
|
550
|
+
modelId,
|
|
551
|
+
options
|
|
552
|
+
}) {
|
|
553
|
+
var _a;
|
|
554
|
+
if (((_a = options.toolChoice) == null ? void 0 : _a.type) !== "required" || !["kimi-k2.6", "kimi-k2.7"].includes(getMoonshotAIModelFamily(modelId))) {
|
|
555
|
+
return { options, warnings: [] };
|
|
556
|
+
}
|
|
557
|
+
return {
|
|
558
|
+
options: { ...options, toolChoice: void 0 },
|
|
559
|
+
warnings: [
|
|
560
|
+
{
|
|
561
|
+
type: "unsupported-setting",
|
|
562
|
+
setting: "toolChoice",
|
|
563
|
+
details: `toolChoice "required" is not supported by model "${modelId}" and has been omitted; use "auto" or select a specific tool instead.`
|
|
564
|
+
}
|
|
565
|
+
]
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
function prepareReasoningOptions({
|
|
569
|
+
modelId,
|
|
570
|
+
options
|
|
571
|
+
}) {
|
|
572
|
+
var _a;
|
|
573
|
+
const providerOptions = (_a = options.providerOptions) == null ? void 0 : _a.moonshotai;
|
|
574
|
+
if (providerOptions == null || typeof providerOptions !== "object" || Array.isArray(providerOptions)) {
|
|
575
|
+
return { options, warnings: [] };
|
|
576
|
+
}
|
|
577
|
+
const moonshotOptions = providerOptions;
|
|
578
|
+
const {
|
|
579
|
+
reasoningEffort: requestedReasoningEffort,
|
|
580
|
+
thinking: requestedThinking,
|
|
581
|
+
reasoningHistory,
|
|
582
|
+
...otherMoonshotOptions
|
|
583
|
+
} = moonshotOptions;
|
|
584
|
+
const preserveReasoning = reasoningHistory === "preserved";
|
|
585
|
+
const warnings = [];
|
|
586
|
+
if ((requestedThinking == null ? void 0 : requestedThinking.budgetTokens) != null) {
|
|
587
|
+
warnings.push({
|
|
588
|
+
type: "other",
|
|
589
|
+
message: "providerOptions.moonshotai.thinking.budgetTokens is deprecated because Moonshot Chat Completions does not support budget_tokens. The option has been omitted."
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
let thinking;
|
|
593
|
+
let reasoningEffort;
|
|
594
|
+
const warnUnsupportedReasoningEffort = () => {
|
|
595
|
+
if (requestedReasoningEffort != null) {
|
|
596
|
+
warnings.push({
|
|
597
|
+
type: "unsupported-setting",
|
|
598
|
+
setting: "providerOptions",
|
|
599
|
+
details: `providerOptions.moonshotai.reasoningEffort is only supported by Kimi K3 and has been omitted for model "${modelId}".`
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
};
|
|
603
|
+
switch (getMoonshotAIModelFamily(modelId)) {
|
|
604
|
+
case "kimi-k3": {
|
|
605
|
+
if (requestedThinking != null) {
|
|
606
|
+
warnings.push({
|
|
607
|
+
type: "unsupported-setting",
|
|
608
|
+
setting: "providerOptions",
|
|
609
|
+
details: "Kimi K3 always reasons and does not accept providerOptions.moonshotai.thinking. The option has been omitted."
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
reasoningEffort = requestedReasoningEffort;
|
|
613
|
+
break;
|
|
614
|
+
}
|
|
615
|
+
case "kimi-k2.7": {
|
|
616
|
+
warnUnsupportedReasoningEffort();
|
|
617
|
+
if ((requestedThinking == null ? void 0 : requestedThinking.type) === "disabled") {
|
|
618
|
+
warnings.push({
|
|
619
|
+
type: "unsupported-setting",
|
|
620
|
+
setting: "providerOptions",
|
|
621
|
+
details: "Kimi K2.7 thinking cannot be disabled. providerOptions.moonshotai.thinking has been omitted."
|
|
622
|
+
});
|
|
623
|
+
} else if ((requestedThinking == null ? void 0 : requestedThinking.type) === "enabled") {
|
|
624
|
+
thinking = { type: "enabled" };
|
|
625
|
+
}
|
|
626
|
+
break;
|
|
627
|
+
}
|
|
628
|
+
case "kimi-k2.6": {
|
|
629
|
+
warnUnsupportedReasoningEffort();
|
|
630
|
+
const thinkingType = requestedThinking == null ? void 0 : requestedThinking.type;
|
|
631
|
+
if (thinkingType != null || preserveReasoning) {
|
|
632
|
+
thinking = {
|
|
633
|
+
type: thinkingType != null ? thinkingType : "enabled",
|
|
634
|
+
...preserveReasoning ? { keep: "all" } : {}
|
|
635
|
+
};
|
|
636
|
+
}
|
|
637
|
+
break;
|
|
638
|
+
}
|
|
639
|
+
case "kimi-k2.5": {
|
|
640
|
+
warnUnsupportedReasoningEffort();
|
|
641
|
+
if ((requestedThinking == null ? void 0 : requestedThinking.type) != null) {
|
|
642
|
+
thinking = { type: requestedThinking.type };
|
|
643
|
+
}
|
|
644
|
+
if (preserveReasoning) {
|
|
645
|
+
warnings.push({
|
|
646
|
+
type: "unsupported-setting",
|
|
647
|
+
setting: "providerOptions",
|
|
648
|
+
details: `providerOptions.moonshotai.reasoningHistory 'preserved' is not supported by model "${modelId}" and has been omitted.`
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
break;
|
|
652
|
+
}
|
|
653
|
+
case "moonshot-v1": {
|
|
654
|
+
warnUnsupportedReasoningEffort();
|
|
655
|
+
if (requestedThinking != null) {
|
|
656
|
+
warnings.push({
|
|
657
|
+
type: "unsupported-setting",
|
|
658
|
+
setting: "providerOptions",
|
|
659
|
+
details: `providerOptions.moonshotai.thinking is not supported by model "${modelId}" and has been omitted.`
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
if (preserveReasoning) {
|
|
663
|
+
warnings.push({
|
|
664
|
+
type: "unsupported-setting",
|
|
665
|
+
setting: "providerOptions",
|
|
666
|
+
details: `providerOptions.moonshotai.reasoningHistory 'preserved' is not supported by model "${modelId}" and has been omitted.`
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
break;
|
|
670
|
+
}
|
|
671
|
+
case "unknown": {
|
|
672
|
+
reasoningEffort = requestedReasoningEffort;
|
|
673
|
+
if ((requestedThinking == null ? void 0 : requestedThinking.type) != null) {
|
|
674
|
+
thinking = { type: requestedThinking.type };
|
|
675
|
+
}
|
|
676
|
+
if (preserveReasoning) {
|
|
677
|
+
warnings.push({
|
|
678
|
+
type: "unsupported-setting",
|
|
679
|
+
setting: "providerOptions",
|
|
680
|
+
details: `providerOptions.moonshotai.reasoningHistory 'preserved' is not supported by model "${modelId}" and has been omitted.`
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
break;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
return {
|
|
687
|
+
options: {
|
|
688
|
+
...options,
|
|
689
|
+
providerOptions: {
|
|
690
|
+
...options.providerOptions,
|
|
691
|
+
moonshotai: {
|
|
692
|
+
...otherMoonshotOptions,
|
|
693
|
+
...reasoningEffort != null ? { reasoningEffort } : {},
|
|
694
|
+
...thinking != null ? { thinking } : {}
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
},
|
|
698
|
+
warnings
|
|
699
|
+
};
|
|
700
|
+
}
|
|
37
701
|
var MoonshotAIChatLanguageModel = class extends OpenAICompatibleChatLanguageModel {
|
|
38
702
|
constructor(modelId, config) {
|
|
39
|
-
super(modelId,
|
|
703
|
+
super(modelId, {
|
|
704
|
+
...config,
|
|
705
|
+
metadataExtractor: moonshotAIChatMetadataExtractor,
|
|
706
|
+
transformRequestBody: (args) => {
|
|
707
|
+
var _a, _b;
|
|
708
|
+
return transformMoonshotRequestBody(
|
|
709
|
+
(_b = (_a = config.transformRequestBody) == null ? void 0 : _a.call(config, args)) != null ? _b : args
|
|
710
|
+
);
|
|
711
|
+
}
|
|
712
|
+
});
|
|
40
713
|
}
|
|
41
714
|
async doGenerate(options) {
|
|
42
|
-
var _a, _b;
|
|
43
|
-
const
|
|
715
|
+
var _a, _b, _c, _d;
|
|
716
|
+
const { options: messageOptions, warnings: messageWarnings } = await prepareMessageOptions({ modelId: this.modelId, options });
|
|
717
|
+
await validateProviderOptions(messageOptions);
|
|
718
|
+
const { options: samplingOptions, warnings: samplingWarnings } = prepareSamplingOptions({
|
|
719
|
+
modelId: this.modelId,
|
|
720
|
+
options: messageOptions
|
|
721
|
+
});
|
|
722
|
+
const { options: sanitizedOptions, warnings: reasoningWarnings } = prepareReasoningOptions({
|
|
723
|
+
modelId: this.modelId,
|
|
724
|
+
options: samplingOptions
|
|
725
|
+
});
|
|
726
|
+
const { options: toolChoiceOptions, warnings: toolChoiceWarnings } = prepareToolChoiceOptions({
|
|
727
|
+
modelId: this.modelId,
|
|
728
|
+
options: sanitizedOptions
|
|
729
|
+
});
|
|
730
|
+
const result = await super.doGenerate(toolChoiceOptions);
|
|
44
731
|
const usage = (_b = (_a = result.response) == null ? void 0 : _a.body) == null ? void 0 : _b.usage;
|
|
732
|
+
const logprobs = extractMoonshotAIChatLogprobs((_c = result.response) == null ? void 0 : _c.body);
|
|
45
733
|
return {
|
|
46
734
|
...result,
|
|
47
|
-
usage: convertMoonshotAIChatUsage(usage)
|
|
735
|
+
usage: convertMoonshotAIChatUsage(usage),
|
|
736
|
+
providerMetadata: {
|
|
737
|
+
...result.providerMetadata,
|
|
738
|
+
moonshotai: {
|
|
739
|
+
...(_d = result.providerMetadata) == null ? void 0 : _d.moonshotai,
|
|
740
|
+
...logprobs != null && { logprobs }
|
|
741
|
+
}
|
|
742
|
+
},
|
|
743
|
+
warnings: [
|
|
744
|
+
...result.warnings,
|
|
745
|
+
...messageWarnings,
|
|
746
|
+
...samplingWarnings,
|
|
747
|
+
...reasoningWarnings,
|
|
748
|
+
...toolChoiceWarnings
|
|
749
|
+
]
|
|
48
750
|
};
|
|
49
751
|
}
|
|
50
752
|
async doStream(options) {
|
|
753
|
+
const { options: messageOptions, warnings: messageWarnings } = await prepareMessageOptions({ modelId: this.modelId, options });
|
|
754
|
+
await validateProviderOptions(messageOptions);
|
|
51
755
|
const originalIncludeRawChunks = options.includeRawChunks;
|
|
756
|
+
const { options: samplingOptions, warnings: samplingWarnings } = prepareSamplingOptions({
|
|
757
|
+
modelId: this.modelId,
|
|
758
|
+
options: messageOptions
|
|
759
|
+
});
|
|
760
|
+
const { options: sanitizedOptions, warnings: reasoningWarnings } = prepareReasoningOptions({
|
|
761
|
+
modelId: this.modelId,
|
|
762
|
+
options: samplingOptions
|
|
763
|
+
});
|
|
764
|
+
const { options: toolChoiceOptions, warnings: toolChoiceWarnings } = prepareToolChoiceOptions({
|
|
765
|
+
modelId: this.modelId,
|
|
766
|
+
options: sanitizedOptions
|
|
767
|
+
});
|
|
52
768
|
const result = await super.doStream({
|
|
53
|
-
...
|
|
769
|
+
...toolChoiceOptions,
|
|
54
770
|
includeRawChunks: true
|
|
55
771
|
});
|
|
56
772
|
let rawUsage = void 0;
|
|
773
|
+
const contentLogprobs = [];
|
|
57
774
|
return {
|
|
58
775
|
...result,
|
|
59
776
|
stream: result.stream.pipeThrough(
|
|
60
777
|
new TransformStream({
|
|
61
778
|
transform(chunk, controller) {
|
|
779
|
+
var _a;
|
|
780
|
+
if (chunk.type === "stream-start") {
|
|
781
|
+
controller.enqueue({
|
|
782
|
+
...chunk,
|
|
783
|
+
warnings: [
|
|
784
|
+
...chunk.warnings,
|
|
785
|
+
...messageWarnings,
|
|
786
|
+
...samplingWarnings,
|
|
787
|
+
...reasoningWarnings,
|
|
788
|
+
...toolChoiceWarnings
|
|
789
|
+
]
|
|
790
|
+
});
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
62
793
|
if (chunk.type === "raw") {
|
|
63
794
|
const rawValue = chunk.rawValue;
|
|
64
795
|
if ((rawValue == null ? void 0 : rawValue.usage) != null) {
|
|
65
796
|
rawUsage = rawValue.usage;
|
|
66
797
|
}
|
|
798
|
+
const logprobs = extractMoonshotAIChatLogprobs(rawValue);
|
|
799
|
+
if ((logprobs == null ? void 0 : logprobs.content) != null) {
|
|
800
|
+
contentLogprobs.push(...logprobs.content);
|
|
801
|
+
}
|
|
67
802
|
if (originalIncludeRawChunks) {
|
|
68
803
|
controller.enqueue(chunk);
|
|
69
804
|
}
|
|
@@ -72,7 +807,16 @@ var MoonshotAIChatLanguageModel = class extends OpenAICompatibleChatLanguageMode
|
|
|
72
807
|
if (chunk.type === "finish") {
|
|
73
808
|
controller.enqueue({
|
|
74
809
|
...chunk,
|
|
75
|
-
usage: rawUsage ? convertMoonshotAIChatUsage(rawUsage) : chunk.usage
|
|
810
|
+
usage: rawUsage ? convertMoonshotAIChatUsage(rawUsage) : chunk.usage,
|
|
811
|
+
providerMetadata: {
|
|
812
|
+
...chunk.providerMetadata,
|
|
813
|
+
moonshotai: {
|
|
814
|
+
...(_a = chunk.providerMetadata) == null ? void 0 : _a.moonshotai,
|
|
815
|
+
...contentLogprobs.length > 0 && {
|
|
816
|
+
logprobs: { content: contentLogprobs }
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
}
|
|
76
820
|
});
|
|
77
821
|
return;
|
|
78
822
|
}
|
|
@@ -85,13 +829,14 @@ var MoonshotAIChatLanguageModel = class extends OpenAICompatibleChatLanguageMode
|
|
|
85
829
|
};
|
|
86
830
|
|
|
87
831
|
// src/version.ts
|
|
88
|
-
var VERSION = true ? "0.0.
|
|
832
|
+
var VERSION = true ? "0.0.24" : "0.0.0-test";
|
|
89
833
|
|
|
90
834
|
// src/moonshotai-provider.ts
|
|
91
|
-
var moonshotaiErrorSchema =
|
|
92
|
-
error:
|
|
93
|
-
message:
|
|
94
|
-
type:
|
|
835
|
+
var moonshotaiErrorSchema = z3.object({
|
|
836
|
+
error: z3.object({
|
|
837
|
+
message: z3.string(),
|
|
838
|
+
type: z3.string().nullish(),
|
|
839
|
+
code: z3.string().nullish()
|
|
95
840
|
})
|
|
96
841
|
});
|
|
97
842
|
var moonshotaiErrorStructure = {
|
|
@@ -99,6 +844,9 @@ var moonshotaiErrorStructure = {
|
|
|
99
844
|
errorToMessage: (data) => data.error.message
|
|
100
845
|
};
|
|
101
846
|
var defaultBaseURL = "https://api.moonshot.ai/v1";
|
|
847
|
+
function getModelStructuredOutputSupport(modelId) {
|
|
848
|
+
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";
|
|
849
|
+
}
|
|
102
850
|
function createMoonshotAI(options = {}) {
|
|
103
851
|
var _a;
|
|
104
852
|
const baseURL = withoutTrailingSlash((_a = options.baseURL) != null ? _a : defaultBaseURL);
|
|
@@ -123,7 +871,8 @@ function createMoonshotAI(options = {}) {
|
|
|
123
871
|
return new MoonshotAIChatLanguageModel(modelId, {
|
|
124
872
|
...getCommonModelConfig("chat"),
|
|
125
873
|
includeUsage: true,
|
|
126
|
-
errorStructure: moonshotaiErrorStructure
|
|
874
|
+
errorStructure: moonshotaiErrorStructure,
|
|
875
|
+
supportsStructuredOutputs: getModelStructuredOutputSupport(modelId)
|
|
127
876
|
});
|
|
128
877
|
};
|
|
129
878
|
const provider = (modelId) => createChatModel(modelId);
|