@ai-sdk/moonshotai 2.0.51 → 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 +21 -0
- package/README.md +44 -10
- package/dist/index.d.mts +41 -3
- package/dist/index.d.ts +41 -3
- package/dist/index.js +528 -271
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +532 -273
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/convert-to-moonshotai-chat-messages.ts +146 -6
- package/src/index.ts +3 -0
- package/src/moonshotai-chat-api-types.ts +56 -6
- package/src/moonshotai-chat-language-model.ts +79 -3
- package/src/moonshotai-chat-options.ts +125 -9
- package/src/moonshotai-prepare-tools.ts +3 -20
package/dist/index.js
CHANGED
|
@@ -34,8 +34,292 @@ 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
|
+
// 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
|
+
}
|
|
39
323
|
var supportedImageMediaTypes = [
|
|
40
324
|
"image/jpeg",
|
|
41
325
|
"image/png",
|
|
@@ -63,26 +347,81 @@ function validateMediaType({
|
|
|
63
347
|
}) {
|
|
64
348
|
const normalizedMediaType = mediaType === `${topLevelMediaType}/*` ? topLevelMediaType === "image" ? "image/jpeg" : "video/mp4" : mediaType;
|
|
65
349
|
if (!supportedMediaTypes.includes(normalizedMediaType)) {
|
|
66
|
-
throw new
|
|
350
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
|
67
351
|
functionality: `file part media type ${normalizedMediaType}`
|
|
68
352
|
});
|
|
69
353
|
}
|
|
70
354
|
return normalizedMediaType;
|
|
71
355
|
}
|
|
72
|
-
function convertToMoonshotAIChatMessages(
|
|
73
|
-
|
|
356
|
+
function convertToMoonshotAIChatMessages({
|
|
357
|
+
modelId,
|
|
358
|
+
prompt,
|
|
359
|
+
providerOptionsName = "moonshotai",
|
|
360
|
+
responseFormat
|
|
361
|
+
}) {
|
|
362
|
+
var _a, _b;
|
|
74
363
|
const messages = [];
|
|
75
|
-
|
|
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
|
+
}
|
|
76
383
|
switch (role) {
|
|
77
384
|
case "system": {
|
|
78
|
-
|
|
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
|
+
});
|
|
79
415
|
break;
|
|
80
416
|
}
|
|
81
417
|
case "user": {
|
|
82
418
|
if (content.length === 1 && content[0].type === "text") {
|
|
83
419
|
messages.push({
|
|
84
420
|
role: "user",
|
|
85
|
-
content: content[0].text
|
|
421
|
+
content: content[0].text,
|
|
422
|
+
...(moonshotMessageOptions == null ? void 0 : moonshotMessageOptions.name) != null && {
|
|
423
|
+
name: moonshotMessageOptions.name
|
|
424
|
+
}
|
|
86
425
|
});
|
|
87
426
|
break;
|
|
88
427
|
}
|
|
@@ -129,16 +468,33 @@ function convertToMoonshotAIChatMessages(prompt) {
|
|
|
129
468
|
text: textContent
|
|
130
469
|
};
|
|
131
470
|
}
|
|
132
|
-
throw new
|
|
471
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
|
133
472
|
functionality: `file part media type ${part.mediaType}`
|
|
134
473
|
});
|
|
135
474
|
}
|
|
136
475
|
}
|
|
137
|
-
})
|
|
476
|
+
}),
|
|
477
|
+
...(moonshotMessageOptions == null ? void 0 : moonshotMessageOptions.name) != null && {
|
|
478
|
+
name: moonshotMessageOptions.name
|
|
479
|
+
}
|
|
138
480
|
});
|
|
139
481
|
break;
|
|
140
482
|
}
|
|
141
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
|
+
}
|
|
142
498
|
let text = "";
|
|
143
499
|
let reasoning = "";
|
|
144
500
|
const toolCalls = [];
|
|
@@ -168,12 +524,24 @@ function convertToMoonshotAIChatMessages(prompt) {
|
|
|
168
524
|
messages.push({
|
|
169
525
|
role: "assistant",
|
|
170
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
|
+
},
|
|
171
533
|
...reasoning.length > 0 ? { reasoning_content: reasoning } : {},
|
|
172
534
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
|
|
173
535
|
});
|
|
174
536
|
break;
|
|
175
537
|
}
|
|
176
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
|
+
}
|
|
177
545
|
for (const toolResponse of content) {
|
|
178
546
|
if (toolResponse.type === "tool-approval-response") {
|
|
179
547
|
continue;
|
|
@@ -186,7 +554,7 @@ function convertToMoonshotAIChatMessages(prompt) {
|
|
|
186
554
|
contentValue = output.value;
|
|
187
555
|
break;
|
|
188
556
|
case "execution-denied":
|
|
189
|
-
contentValue = (
|
|
557
|
+
contentValue = (_b = output.reason) != null ? _b : "Tool call execution denied.";
|
|
190
558
|
break;
|
|
191
559
|
case "content":
|
|
192
560
|
case "json":
|
|
@@ -208,7 +576,7 @@ function convertToMoonshotAIChatMessages(prompt) {
|
|
|
208
576
|
}
|
|
209
577
|
}
|
|
210
578
|
}
|
|
211
|
-
return messages;
|
|
579
|
+
return { messages, warnings };
|
|
212
580
|
}
|
|
213
581
|
|
|
214
582
|
// src/convert-moonshotai-chat-usage.ts
|
|
@@ -282,75 +650,99 @@ function mapMoonshotAIFinishReason(finishReason) {
|
|
|
282
650
|
|
|
283
651
|
// src/moonshotai-chat-api-types.ts
|
|
284
652
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
285
|
-
var
|
|
286
|
-
var tokenUsageSchema =
|
|
287
|
-
prompt_tokens:
|
|
288
|
-
completion_tokens:
|
|
289
|
-
cached_tokens:
|
|
290
|
-
total_tokens:
|
|
291
|
-
prompt_tokens_details:
|
|
292
|
-
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()
|
|
293
661
|
}).nullish(),
|
|
294
|
-
completion_tokens_details:
|
|
295
|
-
reasoning_tokens:
|
|
662
|
+
completion_tokens_details: import_v42.z.looseObject({
|
|
663
|
+
reasoning_tokens: import_v42.z.number().nullish()
|
|
296
664
|
}).nullish()
|
|
297
665
|
}).nullish();
|
|
298
|
-
var moonshotAIErrorSchema =
|
|
299
|
-
error:
|
|
300
|
-
message:
|
|
301
|
-
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()
|
|
302
671
|
})
|
|
303
672
|
});
|
|
304
|
-
var
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
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()
|
|
320
707
|
})
|
|
321
708
|
})
|
|
322
709
|
).nullish()
|
|
323
710
|
}),
|
|
324
|
-
|
|
711
|
+
logprobs: moonshotAIChatLogprobsSchema,
|
|
712
|
+
finish_reason: import_v42.z.string().nullish()
|
|
325
713
|
})
|
|
326
714
|
),
|
|
327
715
|
usage: tokenUsageSchema
|
|
328
716
|
});
|
|
329
717
|
var moonshotAIChatChunkSchema = (0, import_provider_utils2.lazySchema)(
|
|
330
718
|
() => (0, import_provider_utils2.zodSchema)(
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
id:
|
|
334
|
-
created:
|
|
335
|
-
model:
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
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()
|
|
349
740
|
})
|
|
350
741
|
})
|
|
351
742
|
).nullish()
|
|
352
743
|
}).nullish(),
|
|
353
|
-
|
|
744
|
+
logprobs: moonshotAIChatLogprobsSchema,
|
|
745
|
+
finish_reason: import_v42.z.string().nullish(),
|
|
354
746
|
usage: tokenUsageSchema
|
|
355
747
|
})
|
|
356
748
|
),
|
|
@@ -361,205 +753,6 @@ var moonshotAIChatChunkSchema = (0, import_provider_utils2.lazySchema)(
|
|
|
361
753
|
)
|
|
362
754
|
);
|
|
363
755
|
|
|
364
|
-
// src/moonshotai-chat-options.ts
|
|
365
|
-
var import_v42 = require("zod/v4");
|
|
366
|
-
function isMoonshotAIKimiModel(modelId) {
|
|
367
|
-
return getMoonshotAIModelFamily(modelId).startsWith("kimi-");
|
|
368
|
-
}
|
|
369
|
-
function getMoonshotAIModelFamily(modelId) {
|
|
370
|
-
if (modelId === "kimi-k2.5") return "kimi-k2.5";
|
|
371
|
-
if (modelId === "kimi-k2.6") return "kimi-k2.6";
|
|
372
|
-
if (modelId === "kimi-k2.7-code" || modelId === "kimi-k2.7-code-highspeed") {
|
|
373
|
-
return "kimi-k2.7";
|
|
374
|
-
}
|
|
375
|
-
if (modelId === "kimi-k3") return "kimi-k3";
|
|
376
|
-
if (modelId.startsWith("moonshot-v1-")) return "moonshot-v1";
|
|
377
|
-
return "unknown";
|
|
378
|
-
}
|
|
379
|
-
var moonshotaiLanguageModelOptions = import_v42.z.object({
|
|
380
|
-
/**
|
|
381
|
-
* Whether to use strict JSON schema validation for structured outputs.
|
|
382
|
-
*
|
|
383
|
-
* @default true
|
|
384
|
-
*/
|
|
385
|
-
strictJsonSchema: import_v42.z.boolean().optional(),
|
|
386
|
-
/**
|
|
387
|
-
* Reasoning effort for Kimi K3.
|
|
388
|
-
*/
|
|
389
|
-
reasoningEffort: import_v42.z.enum(["low", "high", "max"]).optional(),
|
|
390
|
-
thinking: import_v42.z.object({
|
|
391
|
-
type: import_v42.z.enum(["enabled", "disabled"]).optional(),
|
|
392
|
-
// Accepted so existing callers receive a migration warning. It remains
|
|
393
|
-
// in the public compatibility type below as a deprecated property.
|
|
394
|
-
budgetTokens: import_v42.z.number().int().min(1024).optional()
|
|
395
|
-
}).optional(),
|
|
396
|
-
reasoningHistory: import_v42.z.enum(["disabled", "interleaved", "preserved"]).optional(),
|
|
397
|
-
/**
|
|
398
|
-
* Used to cache responses for similar requests to optimize cache hit rates.
|
|
399
|
-
* Typically a session or task id.
|
|
400
|
-
*/
|
|
401
|
-
promptCacheKey: import_v42.z.string().optional(),
|
|
402
|
-
/**
|
|
403
|
-
* A stable identifier used to help Moonshot detect users violating usage
|
|
404
|
-
* policies. Recommended to hash the username or email address.
|
|
405
|
-
*/
|
|
406
|
-
safetyIdentifier: import_v42.z.string().optional()
|
|
407
|
-
});
|
|
408
|
-
|
|
409
|
-
// src/normalize-json-schema-for-mfjs.ts
|
|
410
|
-
var import_provider2 = require("@ai-sdk/provider");
|
|
411
|
-
function isRecord(value) {
|
|
412
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
413
|
-
}
|
|
414
|
-
var SCHEMA_ARRAY_KEYS = ["allOf", "anyOf", "oneOf", "prefixItems"];
|
|
415
|
-
var SCHEMA_MAP_KEYS = [
|
|
416
|
-
"properties",
|
|
417
|
-
"patternProperties",
|
|
418
|
-
"$defs",
|
|
419
|
-
"dependentSchemas"
|
|
420
|
-
];
|
|
421
|
-
var SCHEMA_SINGLE_KEYS = [
|
|
422
|
-
"additionalProperties",
|
|
423
|
-
"propertyNames",
|
|
424
|
-
"items",
|
|
425
|
-
"contains",
|
|
426
|
-
"not",
|
|
427
|
-
"if",
|
|
428
|
-
"then",
|
|
429
|
-
"else"
|
|
430
|
-
];
|
|
431
|
-
function normalizeJsonSchemaForMFJS(schema) {
|
|
432
|
-
return normalizeDefinition(schema, true);
|
|
433
|
-
}
|
|
434
|
-
function normalizeDefinition(definition, isRoot) {
|
|
435
|
-
if (typeof definition === "boolean" || !isRecord(definition)) {
|
|
436
|
-
if (isRoot) {
|
|
437
|
-
throw new import_provider2.UnsupportedFunctionalityError({
|
|
438
|
-
functionality: 'tool parameters must be a JSON Schema object with type "object" for moonshotai (MFJS)'
|
|
439
|
-
});
|
|
440
|
-
}
|
|
441
|
-
return definition;
|
|
442
|
-
}
|
|
443
|
-
if (isRoot && definition.type !== "object") {
|
|
444
|
-
throw new import_provider2.UnsupportedFunctionalityError({
|
|
445
|
-
functionality: 'tool parameters must be a JSON Schema object with type "object" for moonshotai (MFJS)'
|
|
446
|
-
});
|
|
447
|
-
}
|
|
448
|
-
const result = { ...definition };
|
|
449
|
-
if (Array.isArray(result.items)) {
|
|
450
|
-
const tuple = result.items;
|
|
451
|
-
result.prefixItems = [
|
|
452
|
-
...Array.isArray(result.prefixItems) ? result.prefixItems : [],
|
|
453
|
-
...tuple.map((item) => normalizeDefinition(item, false))
|
|
454
|
-
];
|
|
455
|
-
delete result.items;
|
|
456
|
-
} else if (isRecord(result.items)) {
|
|
457
|
-
result.items = normalizeDefinition(result.items, false);
|
|
458
|
-
}
|
|
459
|
-
if (typeof result.type === "string" && Array.isArray(result.anyOf)) {
|
|
460
|
-
const parentType = result.type;
|
|
461
|
-
delete result.type;
|
|
462
|
-
result.anyOf = result.anyOf.map(
|
|
463
|
-
(branch) => isRecord(branch) && branch.type == null ? { type: parentType, ...branch } : branch
|
|
464
|
-
);
|
|
465
|
-
}
|
|
466
|
-
for (const key of SCHEMA_ARRAY_KEYS) {
|
|
467
|
-
const value = result[key];
|
|
468
|
-
if (Array.isArray(value)) {
|
|
469
|
-
result[key] = value.map((item) => normalizeDefinition(item, false));
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
for (const key of SCHEMA_MAP_KEYS) {
|
|
473
|
-
const value = result[key];
|
|
474
|
-
if (isRecord(value)) {
|
|
475
|
-
result[key] = Object.fromEntries(
|
|
476
|
-
Object.entries(value).map(([k, v]) => [
|
|
477
|
-
k,
|
|
478
|
-
normalizeDefinition(v, false)
|
|
479
|
-
])
|
|
480
|
-
);
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
for (const key of SCHEMA_SINGLE_KEYS) {
|
|
484
|
-
const value = result[key];
|
|
485
|
-
if (isRecord(value) || typeof value === "boolean") {
|
|
486
|
-
result[key] = normalizeDefinition(value, false);
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
return result;
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
// src/moonshotai-prepare-tools.ts
|
|
493
|
-
var import_provider3 = require("@ai-sdk/provider");
|
|
494
|
-
function prepareTools({
|
|
495
|
-
tools,
|
|
496
|
-
toolChoice,
|
|
497
|
-
modelId
|
|
498
|
-
}) {
|
|
499
|
-
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
500
|
-
const toolWarnings = [];
|
|
501
|
-
if (tools == null) {
|
|
502
|
-
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
503
|
-
}
|
|
504
|
-
const moonshotTools = [];
|
|
505
|
-
for (const tool of tools) {
|
|
506
|
-
if (tool.type === "provider") {
|
|
507
|
-
toolWarnings.push({
|
|
508
|
-
type: "unsupported",
|
|
509
|
-
feature: `provider-defined tool ${tool.id}`
|
|
510
|
-
});
|
|
511
|
-
} else {
|
|
512
|
-
moonshotTools.push({
|
|
513
|
-
type: "function",
|
|
514
|
-
function: {
|
|
515
|
-
name: tool.name,
|
|
516
|
-
description: tool.description,
|
|
517
|
-
parameters: normalizeJsonSchemaForMFJS(tool.inputSchema),
|
|
518
|
-
...tool.strict != null ? { strict: tool.strict } : {}
|
|
519
|
-
}
|
|
520
|
-
});
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
if (toolChoice == null) {
|
|
524
|
-
return { tools: moonshotTools, toolChoice: void 0, toolWarnings };
|
|
525
|
-
}
|
|
526
|
-
const type = toolChoice.type;
|
|
527
|
-
switch (type) {
|
|
528
|
-
case "auto":
|
|
529
|
-
case "none":
|
|
530
|
-
return { tools: moonshotTools, toolChoice: type, toolWarnings };
|
|
531
|
-
case "required":
|
|
532
|
-
if (modelId === "kimi-k2.6" || modelId === "kimi-k2.7-code" || modelId === "kimi-k2.7-code-highspeed") {
|
|
533
|
-
toolWarnings.push({
|
|
534
|
-
type: "unsupported",
|
|
535
|
-
feature: `tool choice "required" for model "${modelId}"`,
|
|
536
|
-
details: 'Moonshot AI rejects required tool choice for this model. The setting has been omitted; use "auto" or select a specific tool instead.'
|
|
537
|
-
});
|
|
538
|
-
return {
|
|
539
|
-
tools: moonshotTools,
|
|
540
|
-
toolChoice: void 0,
|
|
541
|
-
toolWarnings
|
|
542
|
-
};
|
|
543
|
-
}
|
|
544
|
-
return { tools: moonshotTools, toolChoice: type, toolWarnings };
|
|
545
|
-
case "tool":
|
|
546
|
-
return {
|
|
547
|
-
tools: moonshotTools,
|
|
548
|
-
toolChoice: {
|
|
549
|
-
type: "function",
|
|
550
|
-
function: { name: toolChoice.toolName }
|
|
551
|
-
},
|
|
552
|
-
toolWarnings
|
|
553
|
-
};
|
|
554
|
-
default: {
|
|
555
|
-
const _exhaustiveCheck = type;
|
|
556
|
-
throw new import_provider3.UnsupportedFunctionalityError({
|
|
557
|
-
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
558
|
-
});
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
|
|
563
756
|
// src/moonshotai-chat-language-model.ts
|
|
564
757
|
var MoonshotAIChatLanguageModel = class {
|
|
565
758
|
constructor(modelId, config) {
|
|
@@ -605,7 +798,6 @@ var MoonshotAIChatLanguageModel = class {
|
|
|
605
798
|
providerOptions,
|
|
606
799
|
schema: moonshotaiLanguageModelOptions
|
|
607
800
|
})) != null ? _a : {};
|
|
608
|
-
const messages = convertToMoonshotAIChatMessages(prompt);
|
|
609
801
|
const allWarnings = [];
|
|
610
802
|
if (topK != null) {
|
|
611
803
|
allWarnings.push({ type: "unsupported", feature: "topK" });
|
|
@@ -765,9 +957,20 @@ var MoonshotAIChatLanguageModel = class {
|
|
|
765
957
|
response_format = { type: "json_object" };
|
|
766
958
|
}
|
|
767
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);
|
|
768
967
|
return {
|
|
769
968
|
args: {
|
|
770
969
|
model: this.modelId,
|
|
970
|
+
...(moonshotOptions.logprobs === true || moonshotOptions.topLogprobs != null) && { logprobs: true },
|
|
971
|
+
...moonshotOptions.topLogprobs != null && {
|
|
972
|
+
top_logprobs: moonshotOptions.topLogprobs
|
|
973
|
+
},
|
|
771
974
|
max_completion_tokens: maxOutputTokens,
|
|
772
975
|
temperature: supportsSamplingOptions ? temperature : void 0,
|
|
773
976
|
top_p: supportsSamplingOptions ? topP : void 0,
|
|
@@ -778,6 +981,9 @@ var MoonshotAIChatLanguageModel = class {
|
|
|
778
981
|
messages,
|
|
779
982
|
tools: moonshotTools,
|
|
780
983
|
tool_choice: moonshotToolChoice,
|
|
984
|
+
...moonshotOptions.prediction != null && {
|
|
985
|
+
prediction: moonshotOptions.prediction
|
|
986
|
+
},
|
|
781
987
|
...thinking != null ? { thinking } : {},
|
|
782
988
|
...reasoningEffort != null && {
|
|
783
989
|
reasoning_effort: reasoningEffort
|
|
@@ -840,6 +1046,21 @@ var MoonshotAIChatLanguageModel = class {
|
|
|
840
1046
|
raw: (_e = choice.finish_reason) != null ? _e : void 0
|
|
841
1047
|
},
|
|
842
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
|
+
},
|
|
843
1064
|
request: { body: args },
|
|
844
1065
|
response: {
|
|
845
1066
|
...getResponseMetadata(responseBody),
|
|
@@ -880,9 +1101,15 @@ var MoonshotAIChatLanguageModel = class {
|
|
|
880
1101
|
};
|
|
881
1102
|
let topLevelUsage = void 0;
|
|
882
1103
|
let choiceUsage = void 0;
|
|
1104
|
+
const contentLogprobs = [];
|
|
1105
|
+
const providerOptionsName = this.providerOptionsName;
|
|
883
1106
|
let isFirstChunk = true;
|
|
884
1107
|
let isActiveReasoning = false;
|
|
885
1108
|
let isActiveText = false;
|
|
1109
|
+
let responseObject;
|
|
1110
|
+
let choiceIndex;
|
|
1111
|
+
let messageRole;
|
|
1112
|
+
const toolCallTypes = /* @__PURE__ */ new Map();
|
|
886
1113
|
return {
|
|
887
1114
|
stream: response.pipeThrough(
|
|
888
1115
|
new TransformStream({
|
|
@@ -890,7 +1117,7 @@ var MoonshotAIChatLanguageModel = class {
|
|
|
890
1117
|
controller.enqueue({ type: "stream-start", warnings });
|
|
891
1118
|
},
|
|
892
1119
|
transform(chunk, controller) {
|
|
893
|
-
var _a2, _b2, _c, _d, _e, _f, _g;
|
|
1120
|
+
var _a2, _b2, _c, _d, _e, _f, _g, _h;
|
|
894
1121
|
if (options.includeRawChunks) {
|
|
895
1122
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
896
1123
|
}
|
|
@@ -902,7 +1129,7 @@ var MoonshotAIChatLanguageModel = class {
|
|
|
902
1129
|
const value = chunk.value;
|
|
903
1130
|
if ("error" in value) {
|
|
904
1131
|
finishReason = { unified: "error", raw: void 0 };
|
|
905
|
-
controller.enqueue({ type: "error", error: value.error
|
|
1132
|
+
controller.enqueue({ type: "error", error: value.error });
|
|
906
1133
|
return;
|
|
907
1134
|
}
|
|
908
1135
|
if (isFirstChunk) {
|
|
@@ -915,20 +1142,32 @@ var MoonshotAIChatLanguageModel = class {
|
|
|
915
1142
|
if (value.usage != null) {
|
|
916
1143
|
topLevelUsage = value.usage;
|
|
917
1144
|
}
|
|
1145
|
+
if (value.object != null) {
|
|
1146
|
+
responseObject = value.object;
|
|
1147
|
+
}
|
|
918
1148
|
const choice = value.choices[0];
|
|
919
1149
|
if ((choice == null ? void 0 : choice.usage) != null) {
|
|
920
1150
|
choiceUsage = choice.usage;
|
|
921
1151
|
}
|
|
1152
|
+
if ((choice == null ? void 0 : choice.index) != null) {
|
|
1153
|
+
choiceIndex = choice.index;
|
|
1154
|
+
}
|
|
922
1155
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
923
1156
|
finishReason = {
|
|
924
1157
|
unified: mapMoonshotAIFinishReason(choice.finish_reason),
|
|
925
1158
|
raw: choice.finish_reason
|
|
926
1159
|
};
|
|
927
1160
|
}
|
|
1161
|
+
if (((_a2 = choice == null ? void 0 : choice.logprobs) == null ? void 0 : _a2.content) != null) {
|
|
1162
|
+
contentLogprobs.push(...choice.logprobs.content);
|
|
1163
|
+
}
|
|
928
1164
|
if ((choice == null ? void 0 : choice.delta) == null) {
|
|
929
1165
|
return;
|
|
930
1166
|
}
|
|
931
1167
|
const delta = choice.delta;
|
|
1168
|
+
if (delta.role != null) {
|
|
1169
|
+
messageRole = delta.role;
|
|
1170
|
+
}
|
|
932
1171
|
const reasoningContent = delta.reasoning_content;
|
|
933
1172
|
if (reasoningContent) {
|
|
934
1173
|
if (!isActiveReasoning) {
|
|
@@ -974,7 +1213,10 @@ var MoonshotAIChatLanguageModel = class {
|
|
|
974
1213
|
fallbackIndex,
|
|
975
1214
|
toolCallDelta
|
|
976
1215
|
] of delta.tool_calls.entries()) {
|
|
977
|
-
const index = (
|
|
1216
|
+
const index = (_b2 = toolCallDelta.index) != null ? _b2 : fallbackIndex;
|
|
1217
|
+
if (toolCallDelta.type != null) {
|
|
1218
|
+
toolCallTypes.set(index, toolCallDelta.type);
|
|
1219
|
+
}
|
|
978
1220
|
if (toolCalls[index] == null) {
|
|
979
1221
|
if (toolCallDelta.id == null) {
|
|
980
1222
|
throw new import_provider4.InvalidResponseDataError({
|
|
@@ -982,7 +1224,7 @@ var MoonshotAIChatLanguageModel = class {
|
|
|
982
1224
|
message: `Expected 'id' to be a string.`
|
|
983
1225
|
});
|
|
984
1226
|
}
|
|
985
|
-
if (((
|
|
1227
|
+
if (((_c = toolCallDelta.function) == null ? void 0 : _c.name) == null) {
|
|
986
1228
|
throw new import_provider4.InvalidResponseDataError({
|
|
987
1229
|
data: toolCallDelta,
|
|
988
1230
|
message: `Expected 'function.name' to be a string.`
|
|
@@ -998,12 +1240,12 @@ var MoonshotAIChatLanguageModel = class {
|
|
|
998
1240
|
type: "function",
|
|
999
1241
|
function: {
|
|
1000
1242
|
name: toolCallDelta.function.name,
|
|
1001
|
-
arguments: (
|
|
1243
|
+
arguments: (_d = toolCallDelta.function.arguments) != null ? _d : ""
|
|
1002
1244
|
},
|
|
1003
1245
|
hasFinished: false
|
|
1004
1246
|
};
|
|
1005
1247
|
const toolCall2 = toolCalls[index];
|
|
1006
|
-
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) {
|
|
1007
1249
|
controller.enqueue({
|
|
1008
1250
|
type: "tool-input-delta",
|
|
1009
1251
|
id: toolCall2.id,
|
|
@@ -1016,13 +1258,13 @@ var MoonshotAIChatLanguageModel = class {
|
|
|
1016
1258
|
if (toolCall.hasFinished) {
|
|
1017
1259
|
continue;
|
|
1018
1260
|
}
|
|
1019
|
-
if (((
|
|
1261
|
+
if (((_g = toolCallDelta.function) == null ? void 0 : _g.arguments) != null) {
|
|
1020
1262
|
toolCall.function.arguments += toolCallDelta.function.arguments;
|
|
1021
1263
|
}
|
|
1022
1264
|
controller.enqueue({
|
|
1023
1265
|
type: "tool-input-delta",
|
|
1024
1266
|
id: toolCall.id,
|
|
1025
|
-
delta: (
|
|
1267
|
+
delta: (_h = toolCallDelta.function.arguments) != null ? _h : ""
|
|
1026
1268
|
});
|
|
1027
1269
|
}
|
|
1028
1270
|
}
|
|
@@ -1051,7 +1293,22 @@ var MoonshotAIChatLanguageModel = class {
|
|
|
1051
1293
|
controller.enqueue({
|
|
1052
1294
|
type: "finish",
|
|
1053
1295
|
finishReason,
|
|
1054
|
-
usage: convertMoonshotAIChatUsage(topLevelUsage != null ? topLevelUsage : choiceUsage)
|
|
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
|
+
}
|
|
1055
1312
|
});
|
|
1056
1313
|
}
|
|
1057
1314
|
})
|
|
@@ -1063,7 +1320,7 @@ var MoonshotAIChatLanguageModel = class {
|
|
|
1063
1320
|
};
|
|
1064
1321
|
|
|
1065
1322
|
// src/version.ts
|
|
1066
|
-
var VERSION = true ? "2.0.
|
|
1323
|
+
var VERSION = true ? "2.0.53" : "0.0.0-test";
|
|
1067
1324
|
|
|
1068
1325
|
// src/moonshotai-provider.ts
|
|
1069
1326
|
var defaultBaseURL = "https://api.moonshot.ai/v1";
|