@ai-sdk/moonshotai 3.0.60 → 3.0.61
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 +9 -0
- package/dist/index.d.ts +91 -88
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1111 -1370
- package/dist/index.js.map +1 -1
- package/package.json +8 -8
package/dist/index.js
CHANGED
|
@@ -1,1405 +1,1146 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
NoSuchModelError
|
|
4
|
-
} from "@ai-sdk/provider";
|
|
5
|
-
import {
|
|
6
|
-
loadApiKey,
|
|
7
|
-
withoutTrailingSlash,
|
|
8
|
-
withUserAgentSuffix
|
|
9
|
-
} from "@ai-sdk/provider-utils";
|
|
10
|
-
|
|
11
|
-
// src/moonshotai-chat-language-model.ts
|
|
12
|
-
import {
|
|
13
|
-
combineHeaders,
|
|
14
|
-
createEventSourceResponseHandler,
|
|
15
|
-
createJsonErrorResponseHandler,
|
|
16
|
-
createJsonResponseHandler,
|
|
17
|
-
createLanguageModelResponseMetadata as getResponseMetadata,
|
|
18
|
-
createProviderStreamError,
|
|
19
|
-
generateId,
|
|
20
|
-
isCustomReasoning,
|
|
21
|
-
mapReasoningToProviderEffort,
|
|
22
|
-
parseProviderOptions as parseProviderOptions2,
|
|
23
|
-
postJsonToApi,
|
|
24
|
-
serializeModelOptions,
|
|
25
|
-
StreamingToolCallTracker,
|
|
26
|
-
WORKFLOW_SERIALIZE,
|
|
27
|
-
WORKFLOW_DESERIALIZE
|
|
28
|
-
} from "@ai-sdk/provider-utils";
|
|
29
|
-
|
|
30
|
-
// src/convert-to-moonshotai-chat-messages.ts
|
|
31
|
-
import {
|
|
32
|
-
InvalidPromptError,
|
|
33
|
-
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
34
|
-
} from "@ai-sdk/provider";
|
|
35
|
-
import {
|
|
36
|
-
convertBase64ToUint8Array,
|
|
37
|
-
convertToBase64,
|
|
38
|
-
getTopLevelMediaType,
|
|
39
|
-
parseProviderOptions,
|
|
40
|
-
resolveProviderReference,
|
|
41
|
-
resolveFullMediaType
|
|
42
|
-
} from "@ai-sdk/provider-utils";
|
|
43
|
-
|
|
44
|
-
// src/moonshotai-chat-options.ts
|
|
1
|
+
import { InvalidPromptError, NoSuchModelError, UnsupportedFunctionalityError } from "@ai-sdk/provider";
|
|
2
|
+
import { StreamingToolCallTracker, WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, convertBase64ToUint8Array, convertToBase64, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, createLanguageModelResponseMetadata, createNullLanguageModelUsage, createProviderStreamError, generateId, getTopLevelMediaType, isCustomReasoning, isRecord, lazySchema, loadApiKey, mapReasoningToProviderEffort, parseProviderOptions, postJsonToApi, resolveFullMediaType, resolveProviderReference, serializeModelOptions, withUserAgentSuffix, withoutTrailingSlash, zodSchema } from "@ai-sdk/provider-utils";
|
|
45
3
|
import { z } from "zod/v4";
|
|
4
|
+
//#region src/moonshotai-chat-options.ts
|
|
46
5
|
function isMoonshotAIKimiModel(modelId) {
|
|
47
|
-
|
|
6
|
+
return getMoonshotAIModelFamily(modelId).startsWith("kimi-");
|
|
48
7
|
}
|
|
49
8
|
function getMoonshotAIModelFamily(modelId) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (modelId.startsWith("moonshot-v1-")) return "moonshot-v1";
|
|
57
|
-
return "unknown";
|
|
9
|
+
if (modelId === "kimi-k2.5") return "kimi-k2.5";
|
|
10
|
+
if (modelId === "kimi-k2.6") return "kimi-k2.6";
|
|
11
|
+
if (modelId === "kimi-k2.7-code" || modelId === "kimi-k2.7-code-highspeed") return "kimi-k2.7";
|
|
12
|
+
if (modelId === "kimi-k3") return "kimi-k3";
|
|
13
|
+
if (modelId.startsWith("moonshot-v1-")) return "moonshot-v1";
|
|
14
|
+
return "unknown";
|
|
58
15
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
});
|
|
132
|
-
var moonshotaiAssistantMessageProviderOptions = moonshotaiMessageProviderOptions.extend({
|
|
133
|
-
/**
|
|
134
|
-
* Whether the assistant message content is a partial response that Moonshot
|
|
135
|
-
* should continue. Only supported on the final assistant message and cannot
|
|
136
|
-
* be combined with JSON object response format.
|
|
137
|
-
*/
|
|
138
|
-
partial: z.literal(true).optional()
|
|
139
|
-
});
|
|
140
|
-
var moonshotaiDynamicToolSchema = z.object({
|
|
141
|
-
type: z.literal("function"),
|
|
142
|
-
name: z.string(),
|
|
143
|
-
description: z.string().optional(),
|
|
144
|
-
inputSchema: z.record(z.string(), z.unknown()),
|
|
145
|
-
strict: z.boolean().optional()
|
|
16
|
+
const moonshotaiLanguageModelOptions = z.object({
|
|
17
|
+
/**
|
|
18
|
+
* Whether to use strict JSON schema validation for structured outputs.
|
|
19
|
+
*
|
|
20
|
+
* @default true
|
|
21
|
+
*/
|
|
22
|
+
strictJsonSchema: z.boolean().optional(),
|
|
23
|
+
/**
|
|
24
|
+
* Whether to return log probabilities for generated tokens.
|
|
25
|
+
*/
|
|
26
|
+
logprobs: z.boolean().optional(),
|
|
27
|
+
/**
|
|
28
|
+
* Number of most likely tokens to return at each token position.
|
|
29
|
+
*
|
|
30
|
+
* Setting this option automatically enables `logprobs`.
|
|
31
|
+
*/
|
|
32
|
+
topLogprobs: z.number().int().min(0).max(20).optional(),
|
|
33
|
+
/**
|
|
34
|
+
* Reasoning effort for Kimi K3. Supports `low`, `high`, and `max`;
|
|
35
|
+
* defaults to `max`.
|
|
36
|
+
*/
|
|
37
|
+
reasoningEffort: z.enum([
|
|
38
|
+
"low",
|
|
39
|
+
"high",
|
|
40
|
+
"max"
|
|
41
|
+
]).optional(),
|
|
42
|
+
/**
|
|
43
|
+
* Static predicted content that can accelerate responses when much of the
|
|
44
|
+
* output is known ahead of time.
|
|
45
|
+
*/
|
|
46
|
+
prediction: z.object({
|
|
47
|
+
type: z.literal("content"),
|
|
48
|
+
content: z.union([z.string(), z.array(z.object({
|
|
49
|
+
type: z.literal("text"),
|
|
50
|
+
text: z.string()
|
|
51
|
+
}))])
|
|
52
|
+
}).optional(),
|
|
53
|
+
/**
|
|
54
|
+
* Thinking configuration for Kimi K2.x models. Kimi K2.5 and K2.6 support
|
|
55
|
+
* enabling or disabling thinking. Kimi K2.7 Code always has thinking
|
|
56
|
+
* enabled.
|
|
57
|
+
*/
|
|
58
|
+
thinking: z.object({
|
|
59
|
+
type: z.enum(["enabled", "disabled"]).optional(),
|
|
60
|
+
/**
|
|
61
|
+
* @deprecated Moonshot Chat Completions does not support thinking
|
|
62
|
+
* budgets. Accepted for backwards compatibility, then omitted with a
|
|
63
|
+
* warning.
|
|
64
|
+
*/
|
|
65
|
+
budgetTokens: z.number().int().min(1024).optional()
|
|
66
|
+
}).optional(),
|
|
67
|
+
/**
|
|
68
|
+
* Controls preserved reasoning behavior in multi-turn conversations.
|
|
69
|
+
* `disabled` and `interleaved` are compatibility values that leave the
|
|
70
|
+
* request unchanged. `preserved` maps to `thinking.keep: 'all'` for Kimi
|
|
71
|
+
* K2.6. Kimi K2.7 and K3 preserve reasoning by default.
|
|
72
|
+
*/
|
|
73
|
+
reasoningHistory: z.enum([
|
|
74
|
+
"disabled",
|
|
75
|
+
"interleaved",
|
|
76
|
+
"preserved"
|
|
77
|
+
]).optional(),
|
|
78
|
+
/**
|
|
79
|
+
* Used to cache responses for similar requests to optimize cache hit rates.
|
|
80
|
+
* Typically a session or task id.
|
|
81
|
+
*/
|
|
82
|
+
promptCacheKey: z.string().optional(),
|
|
83
|
+
/**
|
|
84
|
+
* A stable identifier used to help Moonshot detect users violating usage
|
|
85
|
+
* policies. Recommended to hash the username or email address.
|
|
86
|
+
*/
|
|
87
|
+
safetyIdentifier: z.string().optional()
|
|
146
88
|
});
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
89
|
+
const moonshotaiAssistantMessageProviderOptions = z.object({
|
|
90
|
+
/**
|
|
91
|
+
* The name of the participant represented by the message.
|
|
92
|
+
*
|
|
93
|
+
* Supported on system, user, and assistant messages.
|
|
94
|
+
*/
|
|
95
|
+
name: z.string().optional() }).extend({
|
|
96
|
+
/**
|
|
97
|
+
* Whether the assistant message content is a partial response that Moonshot
|
|
98
|
+
* should continue. Only supported on the final assistant message and cannot
|
|
99
|
+
* be combined with JSON object response format.
|
|
100
|
+
*/
|
|
101
|
+
partial: z.literal(true).optional() });
|
|
102
|
+
const moonshotaiDynamicToolSchema = z.object({
|
|
103
|
+
type: z.literal("function"),
|
|
104
|
+
name: z.string(),
|
|
105
|
+
description: z.string().optional(),
|
|
106
|
+
inputSchema: z.record(z.string(), z.unknown()),
|
|
107
|
+
strict: z.boolean().optional()
|
|
150
108
|
});
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
109
|
+
const moonshotaiAllMessageProviderOptions = moonshotaiAssistantMessageProviderOptions.extend({
|
|
110
|
+
/** Function tools to load at this point in a Kimi K3 conversation. */
|
|
111
|
+
tools: z.array(moonshotaiDynamicToolSchema).optional() });
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region src/normalize-json-schema-for-mfjs.ts
|
|
114
|
+
const SCHEMA_ARRAY_KEYS = [
|
|
115
|
+
"allOf",
|
|
116
|
+
"anyOf",
|
|
117
|
+
"oneOf",
|
|
118
|
+
"prefixItems"
|
|
119
|
+
];
|
|
120
|
+
const SCHEMA_MAP_KEYS = [
|
|
121
|
+
"properties",
|
|
122
|
+
"patternProperties",
|
|
123
|
+
"$defs",
|
|
124
|
+
"dependentSchemas"
|
|
166
125
|
];
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
126
|
+
const SCHEMA_SINGLE_KEYS = [
|
|
127
|
+
"additionalProperties",
|
|
128
|
+
"propertyNames",
|
|
129
|
+
"items",
|
|
130
|
+
"contains",
|
|
131
|
+
"not",
|
|
132
|
+
"if",
|
|
133
|
+
"then",
|
|
134
|
+
"else"
|
|
176
135
|
];
|
|
136
|
+
/**
|
|
137
|
+
* Normalizes a JSON Schema to the subset Moonshot's MFJS validator accepts:
|
|
138
|
+
* `object` root required, tuple `items` become `prefixItems`, and `type` next
|
|
139
|
+
* to `anyOf` moves into the branches. Everything else passes through. The
|
|
140
|
+
* full original schema is still used for AI SDK result validation.
|
|
141
|
+
*/
|
|
177
142
|
function normalizeJsonSchemaForMFJS(schema) {
|
|
178
|
-
|
|
143
|
+
return normalizeDefinition(schema, true);
|
|
179
144
|
}
|
|
180
145
|
function normalizeDefinition(definition, isRoot) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
const value = result[key];
|
|
214
|
-
if (Array.isArray(value)) {
|
|
215
|
-
result[key] = value.map((item) => normalizeDefinition(item, false));
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
for (const key of SCHEMA_MAP_KEYS) {
|
|
219
|
-
const value = result[key];
|
|
220
|
-
if (isRecord(value)) {
|
|
221
|
-
result[key] = Object.fromEntries(
|
|
222
|
-
Object.entries(value).map(([k, v]) => [
|
|
223
|
-
k,
|
|
224
|
-
normalizeDefinition(v, false)
|
|
225
|
-
])
|
|
226
|
-
);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
for (const key of SCHEMA_SINGLE_KEYS) {
|
|
230
|
-
const value = result[key];
|
|
231
|
-
if (isRecord(value) || typeof value === "boolean") {
|
|
232
|
-
result[key] = normalizeDefinition(value, false);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
return result;
|
|
146
|
+
if (typeof definition === "boolean" || !isRecord(definition)) {
|
|
147
|
+
if (isRoot) throw new UnsupportedFunctionalityError({ functionality: "tool parameters must be a JSON Schema object with type \"object\" for moonshotai (MFJS)" });
|
|
148
|
+
return definition;
|
|
149
|
+
}
|
|
150
|
+
if (isRoot && definition.type !== "object") throw new UnsupportedFunctionalityError({ functionality: "tool parameters must be a JSON Schema object with type \"object\" for moonshotai (MFJS)" });
|
|
151
|
+
const result = { ...definition };
|
|
152
|
+
if (Array.isArray(result.items)) {
|
|
153
|
+
const tuple = result.items;
|
|
154
|
+
result.prefixItems = [...Array.isArray(result.prefixItems) ? result.prefixItems : [], ...tuple.map((item) => normalizeDefinition(item, false))];
|
|
155
|
+
delete result.items;
|
|
156
|
+
} else if (isRecord(result.items)) result.items = normalizeDefinition(result.items, false);
|
|
157
|
+
if (typeof result.type === "string" && Array.isArray(result.anyOf)) {
|
|
158
|
+
const parentType = result.type;
|
|
159
|
+
delete result.type;
|
|
160
|
+
result.anyOf = result.anyOf.map((branch) => isRecord(branch) && branch.type == null ? {
|
|
161
|
+
type: parentType,
|
|
162
|
+
...branch
|
|
163
|
+
} : branch);
|
|
164
|
+
}
|
|
165
|
+
for (const key of SCHEMA_ARRAY_KEYS) {
|
|
166
|
+
const value = result[key];
|
|
167
|
+
if (Array.isArray(value)) result[key] = value.map((item) => normalizeDefinition(item, false));
|
|
168
|
+
}
|
|
169
|
+
for (const key of SCHEMA_MAP_KEYS) {
|
|
170
|
+
const value = result[key];
|
|
171
|
+
if (isRecord(value)) result[key] = Object.fromEntries(Object.entries(value).map(([k, v]) => [k, normalizeDefinition(v, false)]));
|
|
172
|
+
}
|
|
173
|
+
for (const key of SCHEMA_SINGLE_KEYS) {
|
|
174
|
+
const value = result[key];
|
|
175
|
+
if (isRecord(value) || typeof value === "boolean") result[key] = normalizeDefinition(value, false);
|
|
176
|
+
}
|
|
177
|
+
return result;
|
|
236
178
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
function prepareTools({
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
303
|
-
});
|
|
304
|
-
}
|
|
305
|
-
}
|
|
179
|
+
//#endregion
|
|
180
|
+
//#region src/moonshotai-prepare-tools.ts
|
|
181
|
+
function prepareTools({ tools, toolChoice, modelId }) {
|
|
182
|
+
tools = tools?.length ? tools : void 0;
|
|
183
|
+
const toolWarnings = [];
|
|
184
|
+
if (tools == null) return {
|
|
185
|
+
tools: void 0,
|
|
186
|
+
toolChoice: void 0,
|
|
187
|
+
toolWarnings
|
|
188
|
+
};
|
|
189
|
+
const moonshotTools = [];
|
|
190
|
+
for (const tool of tools) if (tool.type === "provider") toolWarnings.push({
|
|
191
|
+
type: "unsupported",
|
|
192
|
+
feature: `provider-defined tool ${tool.id}`
|
|
193
|
+
});
|
|
194
|
+
else moonshotTools.push({
|
|
195
|
+
type: "function",
|
|
196
|
+
function: {
|
|
197
|
+
name: tool.name,
|
|
198
|
+
description: tool.description,
|
|
199
|
+
parameters: normalizeJsonSchemaForMFJS(tool.inputSchema),
|
|
200
|
+
...tool.strict != null ? { strict: tool.strict } : {}
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
if (toolChoice == null) return {
|
|
204
|
+
tools: moonshotTools,
|
|
205
|
+
toolChoice: void 0,
|
|
206
|
+
toolWarnings
|
|
207
|
+
};
|
|
208
|
+
const type = toolChoice.type;
|
|
209
|
+
switch (type) {
|
|
210
|
+
case "auto":
|
|
211
|
+
case "none": return {
|
|
212
|
+
tools: moonshotTools,
|
|
213
|
+
toolChoice: type,
|
|
214
|
+
toolWarnings
|
|
215
|
+
};
|
|
216
|
+
case "required":
|
|
217
|
+
if (modelId === "kimi-k2.6" || modelId === "kimi-k2.7-code" || modelId === "kimi-k2.7-code-highspeed") {
|
|
218
|
+
toolWarnings.push({
|
|
219
|
+
type: "unsupported",
|
|
220
|
+
feature: `tool choice "required" for model "${modelId}"`,
|
|
221
|
+
details: "Moonshot AI rejects required tool choice for this model. The setting has been omitted; use \"auto\" or select a specific tool instead."
|
|
222
|
+
});
|
|
223
|
+
return {
|
|
224
|
+
tools: moonshotTools,
|
|
225
|
+
toolChoice: void 0,
|
|
226
|
+
toolWarnings
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
return {
|
|
230
|
+
tools: moonshotTools,
|
|
231
|
+
toolChoice: type,
|
|
232
|
+
toolWarnings
|
|
233
|
+
};
|
|
234
|
+
case "tool": return {
|
|
235
|
+
tools: moonshotTools,
|
|
236
|
+
toolChoice: {
|
|
237
|
+
type: "function",
|
|
238
|
+
function: { name: toolChoice.toolName }
|
|
239
|
+
},
|
|
240
|
+
toolWarnings
|
|
241
|
+
};
|
|
242
|
+
default: throw new UnsupportedFunctionalityError({ functionality: `tool choice type: ${type}` });
|
|
243
|
+
}
|
|
306
244
|
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
245
|
+
//#endregion
|
|
246
|
+
//#region src/convert-to-moonshotai-chat-messages.ts
|
|
247
|
+
const supportedImageMediaTypes = [
|
|
248
|
+
"image/jpeg",
|
|
249
|
+
"image/png",
|
|
250
|
+
"image/gif",
|
|
251
|
+
"image/webp",
|
|
252
|
+
"image/bmp",
|
|
253
|
+
"image/heic",
|
|
254
|
+
"image/heif"
|
|
317
255
|
];
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
256
|
+
const supportedVideoMediaTypes = [
|
|
257
|
+
"video/mp4",
|
|
258
|
+
"video/mpeg",
|
|
259
|
+
"video/mov",
|
|
260
|
+
"video/avi",
|
|
261
|
+
"video/x-flv",
|
|
262
|
+
"video/mpg",
|
|
263
|
+
"video/webm",
|
|
264
|
+
"video/wmv",
|
|
265
|
+
"video/3gpp"
|
|
328
266
|
];
|
|
329
|
-
function formatMediaUrl({
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
if (part.data.type !== "url" && part.data.type !== "data") {
|
|
335
|
-
throw new UnsupportedFunctionalityError3({
|
|
336
|
-
functionality: `file part data type ${part.data.type}`
|
|
337
|
-
});
|
|
338
|
-
}
|
|
339
|
-
const mediaType = part.data.type === "url" ? part.mediaType : resolveFullMediaType({ part });
|
|
340
|
-
if (!supportedMediaTypes.includes(mediaType) && !(part.data.type === "url" && (mediaType === topLevelMediaType || mediaType === `${topLevelMediaType}/*`))) {
|
|
341
|
-
throw new UnsupportedFunctionalityError3({
|
|
342
|
-
functionality: `file part media type ${mediaType}`
|
|
343
|
-
});
|
|
344
|
-
}
|
|
345
|
-
return part.data.type === "url" ? part.data.url.toString() : `data:${mediaType};base64,${convertToBase64(part.data.data)}`;
|
|
267
|
+
function formatMediaUrl({ part, supportedMediaTypes, topLevelMediaType }) {
|
|
268
|
+
if (part.data.type !== "url" && part.data.type !== "data") throw new UnsupportedFunctionalityError({ functionality: `file part data type ${part.data.type}` });
|
|
269
|
+
const mediaType = part.data.type === "url" ? part.mediaType : resolveFullMediaType({ part });
|
|
270
|
+
if (!supportedMediaTypes.includes(mediaType) && !(part.data.type === "url" && (mediaType === topLevelMediaType || mediaType === `${topLevelMediaType}/*`))) throw new UnsupportedFunctionalityError({ functionality: `file part media type ${mediaType}` });
|
|
271
|
+
return part.data.type === "url" ? part.data.url.toString() : `data:${mediaType};base64,${convertToBase64(part.data.data)}`;
|
|
346
272
|
}
|
|
347
|
-
async function convertToMoonshotAIChatMessages({
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
type: "function",
|
|
544
|
-
function: {
|
|
545
|
-
name: part.toolName,
|
|
546
|
-
arguments: JSON.stringify(part.input)
|
|
547
|
-
}
|
|
548
|
-
});
|
|
549
|
-
break;
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
}
|
|
553
|
-
messages.push({
|
|
554
|
-
role: "assistant",
|
|
555
|
-
content: toolCalls.length > 0 ? text || null : text,
|
|
556
|
-
...moonshotMessageOptions?.name != null && {
|
|
557
|
-
name: moonshotMessageOptions.name
|
|
558
|
-
},
|
|
559
|
-
...moonshotMessageOptions?.partial === true && {
|
|
560
|
-
partial: true
|
|
561
|
-
},
|
|
562
|
-
...reasoning.length > 0 ? { reasoning_content: reasoning } : {},
|
|
563
|
-
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
|
|
564
|
-
});
|
|
565
|
-
break;
|
|
566
|
-
}
|
|
567
|
-
case "tool": {
|
|
568
|
-
if (moonshotMessageOptions?.name != null) {
|
|
569
|
-
warnings.push({
|
|
570
|
-
type: "unsupported",
|
|
571
|
-
feature: "message name on tool messages"
|
|
572
|
-
});
|
|
573
|
-
}
|
|
574
|
-
for (const toolResponse of content) {
|
|
575
|
-
if (toolResponse.type === "tool-approval-response") {
|
|
576
|
-
continue;
|
|
577
|
-
}
|
|
578
|
-
const output = toolResponse.output;
|
|
579
|
-
let contentValue;
|
|
580
|
-
switch (output.type) {
|
|
581
|
-
case "text":
|
|
582
|
-
case "error-text":
|
|
583
|
-
contentValue = output.value;
|
|
584
|
-
break;
|
|
585
|
-
case "execution-denied":
|
|
586
|
-
contentValue = output.reason ?? "Tool call execution denied.";
|
|
587
|
-
break;
|
|
588
|
-
case "content":
|
|
589
|
-
case "json":
|
|
590
|
-
case "error-json":
|
|
591
|
-
contentValue = JSON.stringify(output.value);
|
|
592
|
-
break;
|
|
593
|
-
}
|
|
594
|
-
messages.push({
|
|
595
|
-
role: "tool",
|
|
596
|
-
tool_call_id: toolResponse.toolCallId,
|
|
597
|
-
content: contentValue
|
|
598
|
-
});
|
|
599
|
-
}
|
|
600
|
-
break;
|
|
601
|
-
}
|
|
602
|
-
default: {
|
|
603
|
-
const _exhaustiveCheck = role;
|
|
604
|
-
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
return { messages, warnings };
|
|
273
|
+
async function convertToMoonshotAIChatMessages({ modelId, prompt, providerOptionsName = "moonshotai", responseFormat }) {
|
|
274
|
+
const messages = [];
|
|
275
|
+
const warnings = [];
|
|
276
|
+
const modelFamily = modelId == null ? "unknown" : getMoonshotAIModelFamily(modelId);
|
|
277
|
+
for (const [index, { role, content, providerOptions }] of prompt.entries()) {
|
|
278
|
+
const moonshotMessageOptions = await parseProviderOptions({
|
|
279
|
+
provider: providerOptionsName,
|
|
280
|
+
providerOptions,
|
|
281
|
+
schema: moonshotaiAllMessageProviderOptions
|
|
282
|
+
});
|
|
283
|
+
if (moonshotMessageOptions?.partial === true && role !== "assistant") throw new InvalidPromptError({
|
|
284
|
+
prompt,
|
|
285
|
+
message: "Moonshot AI Partial Mode requires `partial: true` on an assistant message."
|
|
286
|
+
});
|
|
287
|
+
if (moonshotMessageOptions?.tools != null && role !== "system") throw new InvalidPromptError({
|
|
288
|
+
prompt,
|
|
289
|
+
message: "Moonshot dynamic tools must be configured on a system message."
|
|
290
|
+
});
|
|
291
|
+
switch (role) {
|
|
292
|
+
case "system":
|
|
293
|
+
if (moonshotMessageOptions?.tools?.length) {
|
|
294
|
+
if (content.length > 0) throw new InvalidPromptError({
|
|
295
|
+
prompt,
|
|
296
|
+
message: "A Moonshot dynamic-tool system message must use empty content because the API forbids content alongside tools."
|
|
297
|
+
});
|
|
298
|
+
if (modelFamily !== "kimi-k3" && modelFamily !== "unknown") {
|
|
299
|
+
warnings.push({
|
|
300
|
+
type: "unsupported",
|
|
301
|
+
feature: `dynamic tool loading for model "${modelId}"`,
|
|
302
|
+
details: "Moonshot documents dynamic tool loading only for Kimi K3. The dynamic system message has been omitted."
|
|
303
|
+
});
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
const { tools, toolWarnings } = prepareTools({
|
|
307
|
+
modelId: modelId ?? "custom-model",
|
|
308
|
+
tools: moonshotMessageOptions.tools
|
|
309
|
+
});
|
|
310
|
+
warnings.push(...toolWarnings);
|
|
311
|
+
messages.push({
|
|
312
|
+
role: "system",
|
|
313
|
+
tools: tools ?? []
|
|
314
|
+
});
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
messages.push({
|
|
318
|
+
role: "system",
|
|
319
|
+
content,
|
|
320
|
+
...moonshotMessageOptions?.name != null && { name: moonshotMessageOptions.name }
|
|
321
|
+
});
|
|
322
|
+
break;
|
|
323
|
+
case "user":
|
|
324
|
+
if (content.length === 1 && content[0].type === "text") {
|
|
325
|
+
messages.push({
|
|
326
|
+
role: "user",
|
|
327
|
+
content: content[0].text,
|
|
328
|
+
...moonshotMessageOptions?.name != null && { name: moonshotMessageOptions.name }
|
|
329
|
+
});
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
messages.push({
|
|
333
|
+
role: "user",
|
|
334
|
+
content: content.map((part) => {
|
|
335
|
+
switch (part.type) {
|
|
336
|
+
case "text": return {
|
|
337
|
+
type: "text",
|
|
338
|
+
text: part.text
|
|
339
|
+
};
|
|
340
|
+
case "file": {
|
|
341
|
+
const topLevel = getTopLevelMediaType(part.mediaType);
|
|
342
|
+
switch (part.data.type) {
|
|
343
|
+
case "reference": {
|
|
344
|
+
if (topLevel !== "image" && topLevel !== "video") throw new UnsupportedFunctionalityError({ functionality: `file part media type ${part.mediaType}` });
|
|
345
|
+
const reference = resolveProviderReference({
|
|
346
|
+
reference: part.data.reference,
|
|
347
|
+
provider: "moonshotai"
|
|
348
|
+
});
|
|
349
|
+
if (!reference.startsWith("ms://")) throw new UnsupportedFunctionalityError({ functionality: "Moonshot file provider references without an ms:// URL" });
|
|
350
|
+
return topLevel === "image" ? {
|
|
351
|
+
type: "image_url",
|
|
352
|
+
image_url: { url: reference }
|
|
353
|
+
} : {
|
|
354
|
+
type: "video_url",
|
|
355
|
+
video_url: { url: reference }
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
case "text":
|
|
359
|
+
if (topLevel === "text") return {
|
|
360
|
+
type: "text",
|
|
361
|
+
text: part.data.text
|
|
362
|
+
};
|
|
363
|
+
throw new UnsupportedFunctionalityError({ functionality: `file part media type ${part.mediaType}` });
|
|
364
|
+
case "url":
|
|
365
|
+
case "data":
|
|
366
|
+
if (topLevel === "image") return {
|
|
367
|
+
type: "image_url",
|
|
368
|
+
image_url: { url: formatMediaUrl({
|
|
369
|
+
part,
|
|
370
|
+
supportedMediaTypes: supportedImageMediaTypes,
|
|
371
|
+
topLevelMediaType: "image"
|
|
372
|
+
}) }
|
|
373
|
+
};
|
|
374
|
+
if (topLevel === "video") return {
|
|
375
|
+
type: "video_url",
|
|
376
|
+
video_url: { url: formatMediaUrl({
|
|
377
|
+
part,
|
|
378
|
+
supportedMediaTypes: supportedVideoMediaTypes,
|
|
379
|
+
topLevelMediaType: "video"
|
|
380
|
+
}) }
|
|
381
|
+
};
|
|
382
|
+
if (topLevel === "text") return {
|
|
383
|
+
type: "text",
|
|
384
|
+
text: part.data.type === "url" ? part.data.url.toString() : typeof part.data.data === "string" ? new TextDecoder().decode(convertBase64ToUint8Array(part.data.data)) : new TextDecoder().decode(part.data.data)
|
|
385
|
+
};
|
|
386
|
+
throw new UnsupportedFunctionalityError({ functionality: `file part media type ${part.mediaType}` });
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}),
|
|
391
|
+
...moonshotMessageOptions?.name != null && { name: moonshotMessageOptions.name }
|
|
392
|
+
});
|
|
393
|
+
break;
|
|
394
|
+
case "assistant": {
|
|
395
|
+
if (moonshotMessageOptions?.partial === true) {
|
|
396
|
+
if (index !== prompt.length - 1) throw new InvalidPromptError({
|
|
397
|
+
prompt,
|
|
398
|
+
message: "Moonshot AI Partial Mode requires the partial assistant message to be the final message."
|
|
399
|
+
});
|
|
400
|
+
if (responseFormat?.type === "json_object") throw new InvalidPromptError({
|
|
401
|
+
prompt,
|
|
402
|
+
message: "Moonshot AI Partial Mode cannot be combined with JSON object response format."
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
let text = "";
|
|
406
|
+
let reasoning = "";
|
|
407
|
+
const toolCalls = [];
|
|
408
|
+
for (const part of content) switch (part.type) {
|
|
409
|
+
case "text":
|
|
410
|
+
text += part.text;
|
|
411
|
+
break;
|
|
412
|
+
case "reasoning":
|
|
413
|
+
reasoning += part.text;
|
|
414
|
+
break;
|
|
415
|
+
case "tool-call": toolCalls.push({
|
|
416
|
+
id: part.toolCallId,
|
|
417
|
+
type: "function",
|
|
418
|
+
function: {
|
|
419
|
+
name: part.toolName,
|
|
420
|
+
arguments: JSON.stringify(part.input)
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
messages.push({
|
|
425
|
+
role: "assistant",
|
|
426
|
+
content: toolCalls.length > 0 ? text || null : text,
|
|
427
|
+
...moonshotMessageOptions?.name != null && { name: moonshotMessageOptions.name },
|
|
428
|
+
...moonshotMessageOptions?.partial === true && { partial: true },
|
|
429
|
+
...reasoning.length > 0 ? { reasoning_content: reasoning } : {},
|
|
430
|
+
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
|
|
431
|
+
});
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
case "tool":
|
|
435
|
+
if (moonshotMessageOptions?.name != null) warnings.push({
|
|
436
|
+
type: "unsupported",
|
|
437
|
+
feature: "message name on tool messages"
|
|
438
|
+
});
|
|
439
|
+
for (const toolResponse of content) {
|
|
440
|
+
if (toolResponse.type === "tool-approval-response") continue;
|
|
441
|
+
const output = toolResponse.output;
|
|
442
|
+
let contentValue;
|
|
443
|
+
switch (output.type) {
|
|
444
|
+
case "text":
|
|
445
|
+
case "error-text":
|
|
446
|
+
contentValue = output.value;
|
|
447
|
+
break;
|
|
448
|
+
case "execution-denied":
|
|
449
|
+
contentValue = output.reason ?? "Tool call execution denied.";
|
|
450
|
+
break;
|
|
451
|
+
case "content":
|
|
452
|
+
case "json":
|
|
453
|
+
case "error-json": contentValue = JSON.stringify(output.value);
|
|
454
|
+
}
|
|
455
|
+
messages.push({
|
|
456
|
+
role: "tool",
|
|
457
|
+
tool_call_id: toolResponse.toolCallId,
|
|
458
|
+
content: contentValue
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
break;
|
|
462
|
+
default: throw new Error(`Unsupported role: ${role}`);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
return {
|
|
466
|
+
messages,
|
|
467
|
+
warnings
|
|
468
|
+
};
|
|
609
469
|
}
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
import { createNullLanguageModelUsage } from "@ai-sdk/provider-utils";
|
|
470
|
+
//#endregion
|
|
471
|
+
//#region src/convert-moonshotai-chat-usage.ts
|
|
613
472
|
function convertMoonshotAIChatUsage(usage) {
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
raw: usage
|
|
634
|
-
};
|
|
473
|
+
if (usage == null) return createNullLanguageModelUsage();
|
|
474
|
+
const promptTokens = usage.prompt_tokens ?? 0;
|
|
475
|
+
const completionTokens = usage.completion_tokens ?? 0;
|
|
476
|
+
const cacheReadTokens = usage.cached_tokens ?? usage.prompt_tokens_details?.cached_tokens ?? 0;
|
|
477
|
+
const reasoningTokens = usage.completion_tokens_details?.reasoning_tokens ?? 0;
|
|
478
|
+
return {
|
|
479
|
+
inputTokens: {
|
|
480
|
+
total: promptTokens,
|
|
481
|
+
noCache: promptTokens - cacheReadTokens,
|
|
482
|
+
cacheRead: cacheReadTokens,
|
|
483
|
+
cacheWrite: void 0
|
|
484
|
+
},
|
|
485
|
+
outputTokens: {
|
|
486
|
+
total: completionTokens,
|
|
487
|
+
text: Math.max(0, completionTokens - reasoningTokens),
|
|
488
|
+
reasoning: reasoningTokens
|
|
489
|
+
},
|
|
490
|
+
raw: usage
|
|
491
|
+
};
|
|
635
492
|
}
|
|
636
|
-
|
|
637
|
-
|
|
493
|
+
//#endregion
|
|
494
|
+
//#region src/map-moonshotai-finish-reason.ts
|
|
638
495
|
function mapMoonshotAIFinishReason(finishReason) {
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
case "tool_calls":
|
|
648
|
-
return "tool-calls";
|
|
649
|
-
default:
|
|
650
|
-
return "other";
|
|
651
|
-
}
|
|
496
|
+
switch (finishReason) {
|
|
497
|
+
case "stop": return "stop";
|
|
498
|
+
case "length": return "length";
|
|
499
|
+
case "content_filter": return "content-filter";
|
|
500
|
+
case "function_call":
|
|
501
|
+
case "tool_calls": return "tool-calls";
|
|
502
|
+
default: return "other";
|
|
503
|
+
}
|
|
652
504
|
}
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
prompt_tokens_details: z2.looseObject({
|
|
663
|
-
cached_tokens: z2.number().nullish()
|
|
664
|
-
}).nullish(),
|
|
665
|
-
completion_tokens_details: z2.looseObject({
|
|
666
|
-
reasoning_tokens: z2.number().nullish()
|
|
667
|
-
}).nullish()
|
|
505
|
+
//#endregion
|
|
506
|
+
//#region src/moonshotai-chat-api-types.ts
|
|
507
|
+
const tokenUsageSchema = z.looseObject({
|
|
508
|
+
prompt_tokens: z.number().nullish(),
|
|
509
|
+
completion_tokens: z.number().nullish(),
|
|
510
|
+
cached_tokens: z.number().nullish(),
|
|
511
|
+
total_tokens: z.number().nullish(),
|
|
512
|
+
prompt_tokens_details: z.looseObject({ cached_tokens: z.number().nullish() }).nullish(),
|
|
513
|
+
completion_tokens_details: z.looseObject({ reasoning_tokens: z.number().nullish() }).nullish()
|
|
668
514
|
}).nullish();
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
515
|
+
const moonshotAIErrorSchema = z.object({ error: z.object({
|
|
516
|
+
message: z.string(),
|
|
517
|
+
type: z.string().nullish(),
|
|
518
|
+
code: z.string().nullish()
|
|
519
|
+
}) });
|
|
520
|
+
const moonshotAIChatLogprobSchema = z.object({
|
|
521
|
+
token: z.string(),
|
|
522
|
+
logprob: z.number(),
|
|
523
|
+
bytes: z.array(z.number()).nullable(),
|
|
524
|
+
top_logprobs: z.array(z.object({
|
|
525
|
+
token: z.string(),
|
|
526
|
+
logprob: z.number(),
|
|
527
|
+
bytes: z.array(z.number()).nullable()
|
|
528
|
+
}))
|
|
675
529
|
});
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
content: z2.string().nullish(),
|
|
702
|
-
reasoning_content: z2.string().nullish(),
|
|
703
|
-
tool_calls: z2.array(
|
|
704
|
-
z2.object({
|
|
705
|
-
id: z2.string().nullish(),
|
|
706
|
-
type: z2.literal("function").nullish(),
|
|
707
|
-
function: z2.object({
|
|
708
|
-
name: z2.string(),
|
|
709
|
-
arguments: z2.string()
|
|
710
|
-
})
|
|
711
|
-
})
|
|
712
|
-
).nullish()
|
|
713
|
-
}),
|
|
714
|
-
logprobs: moonshotAIChatLogprobsSchema,
|
|
715
|
-
finish_reason: z2.string().nullish()
|
|
716
|
-
})
|
|
717
|
-
),
|
|
718
|
-
usage: tokenUsageSchema
|
|
530
|
+
const moonshotAIChatLogprobsSchema = z.object({ content: z.array(moonshotAIChatLogprobSchema).nullish() }).nullish();
|
|
531
|
+
const moonshotAIChatResponseSchema = z.object({
|
|
532
|
+
id: z.string().nullish(),
|
|
533
|
+
created: z.number().nullish(),
|
|
534
|
+
model: z.string().nullish(),
|
|
535
|
+
object: z.literal("chat.completion").nullish(),
|
|
536
|
+
choices: z.array(z.object({
|
|
537
|
+
index: z.number().nullish(),
|
|
538
|
+
message: z.object({
|
|
539
|
+
role: z.literal("assistant").nullish(),
|
|
540
|
+
content: z.string().nullish(),
|
|
541
|
+
reasoning_content: z.string().nullish(),
|
|
542
|
+
tool_calls: z.array(z.object({
|
|
543
|
+
id: z.string().nullish(),
|
|
544
|
+
type: z.literal("function").nullish(),
|
|
545
|
+
function: z.object({
|
|
546
|
+
name: z.string(),
|
|
547
|
+
arguments: z.string()
|
|
548
|
+
})
|
|
549
|
+
})).nullish()
|
|
550
|
+
}),
|
|
551
|
+
logprobs: moonshotAIChatLogprobsSchema,
|
|
552
|
+
finish_reason: z.string().nullish()
|
|
553
|
+
})),
|
|
554
|
+
usage: tokenUsageSchema
|
|
719
555
|
});
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
usage: tokenUsageSchema
|
|
750
|
-
})
|
|
751
|
-
),
|
|
752
|
-
usage: tokenUsageSchema
|
|
753
|
-
}),
|
|
754
|
-
moonshotAIErrorSchema
|
|
755
|
-
])
|
|
756
|
-
)
|
|
757
|
-
);
|
|
758
|
-
|
|
759
|
-
// src/moonshotai-chat-language-model.ts
|
|
556
|
+
const moonshotAIChatChunkSchema = lazySchema(() => zodSchema(z.union([z.object({
|
|
557
|
+
id: z.string().nullish(),
|
|
558
|
+
created: z.number().nullish(),
|
|
559
|
+
model: z.string().nullish(),
|
|
560
|
+
object: z.literal("chat.completion.chunk").nullish(),
|
|
561
|
+
choices: z.array(z.object({
|
|
562
|
+
index: z.number().nullish(),
|
|
563
|
+
delta: z.object({
|
|
564
|
+
role: z.literal("assistant").nullish(),
|
|
565
|
+
content: z.string().nullish(),
|
|
566
|
+
reasoning_content: z.string().nullish(),
|
|
567
|
+
tool_calls: z.array(z.object({
|
|
568
|
+
index: z.number().nullish(),
|
|
569
|
+
id: z.string().nullish(),
|
|
570
|
+
type: z.literal("function").nullish(),
|
|
571
|
+
function: z.object({
|
|
572
|
+
name: z.string().nullish(),
|
|
573
|
+
arguments: z.string().nullish()
|
|
574
|
+
})
|
|
575
|
+
})).nullish()
|
|
576
|
+
}).nullish(),
|
|
577
|
+
logprobs: moonshotAIChatLogprobsSchema,
|
|
578
|
+
finish_reason: z.string().nullish(),
|
|
579
|
+
usage: tokenUsageSchema
|
|
580
|
+
})),
|
|
581
|
+
usage: tokenUsageSchema
|
|
582
|
+
}), moonshotAIErrorSchema])));
|
|
583
|
+
//#endregion
|
|
584
|
+
//#region src/moonshotai-chat-language-model.ts
|
|
760
585
|
function createMoonshotAIStreamError(error, data) {
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
586
|
+
return createProviderStreamError({
|
|
587
|
+
message: error.message,
|
|
588
|
+
type: error.type ?? void 0,
|
|
589
|
+
code: error.code ?? void 0,
|
|
590
|
+
...getMoonshotAIStreamErrorMetadata(error.type),
|
|
591
|
+
data
|
|
592
|
+
});
|
|
768
593
|
}
|
|
769
594
|
function getMoonshotAIStreamErrorMetadata(type) {
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
595
|
+
switch (type) {
|
|
596
|
+
case "rate_limit_exceeded":
|
|
597
|
+
case "rate_limit_error": return {
|
|
598
|
+
statusCode: 429,
|
|
599
|
+
isRetryable: true
|
|
600
|
+
};
|
|
601
|
+
case "server_error":
|
|
602
|
+
case "api_error":
|
|
603
|
+
case "internal_server_error": return {
|
|
604
|
+
statusCode: 500,
|
|
605
|
+
isRetryable: true
|
|
606
|
+
};
|
|
607
|
+
case "overloaded_error":
|
|
608
|
+
case "service_unavailable": return {
|
|
609
|
+
statusCode: 503,
|
|
610
|
+
isRetryable: true
|
|
611
|
+
};
|
|
612
|
+
case "timeout":
|
|
613
|
+
case "timeout_error": return {
|
|
614
|
+
statusCode: 504,
|
|
615
|
+
isRetryable: true
|
|
616
|
+
};
|
|
617
|
+
case "authentication_error":
|
|
618
|
+
case "invalid_api_key": return {
|
|
619
|
+
statusCode: 401,
|
|
620
|
+
isRetryable: false
|
|
621
|
+
};
|
|
622
|
+
case "permission_error": return {
|
|
623
|
+
statusCode: 403,
|
|
624
|
+
isRetryable: false
|
|
625
|
+
};
|
|
626
|
+
case "not_found_error":
|
|
627
|
+
case "model_not_found": return {
|
|
628
|
+
statusCode: 404,
|
|
629
|
+
isRetryable: false
|
|
630
|
+
};
|
|
631
|
+
case "bad_request":
|
|
632
|
+
case "context_length_exceeded":
|
|
633
|
+
case "invalid_request_error": return {
|
|
634
|
+
statusCode: 400,
|
|
635
|
+
isRetryable: false
|
|
636
|
+
};
|
|
637
|
+
default: return {};
|
|
638
|
+
}
|
|
799
639
|
}
|
|
800
|
-
var MoonshotAIChatLanguageModel = class
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
unified: mapMoonshotAIFinishReason(choice.finish_reason),
|
|
1255
|
-
raw: choice.finish_reason
|
|
1256
|
-
};
|
|
1257
|
-
}
|
|
1258
|
-
if (choice?.logprobs?.content != null) {
|
|
1259
|
-
contentLogprobs.push(...choice.logprobs.content);
|
|
1260
|
-
}
|
|
1261
|
-
if (choice?.delta == null) {
|
|
1262
|
-
return;
|
|
1263
|
-
}
|
|
1264
|
-
const delta = choice.delta;
|
|
1265
|
-
if (delta.role != null) {
|
|
1266
|
-
messageRole = delta.role;
|
|
1267
|
-
}
|
|
1268
|
-
const reasoningContent = delta.reasoning_content;
|
|
1269
|
-
if (reasoningContent) {
|
|
1270
|
-
if (!isActiveReasoning) {
|
|
1271
|
-
controller.enqueue({
|
|
1272
|
-
type: "reasoning-start",
|
|
1273
|
-
id: "reasoning-0"
|
|
1274
|
-
});
|
|
1275
|
-
isActiveReasoning = true;
|
|
1276
|
-
}
|
|
1277
|
-
controller.enqueue({
|
|
1278
|
-
type: "reasoning-delta",
|
|
1279
|
-
id: "reasoning-0",
|
|
1280
|
-
delta: reasoningContent
|
|
1281
|
-
});
|
|
1282
|
-
}
|
|
1283
|
-
if (delta.content) {
|
|
1284
|
-
if (!isActiveText) {
|
|
1285
|
-
controller.enqueue({ type: "text-start", id: "txt-0" });
|
|
1286
|
-
isActiveText = true;
|
|
1287
|
-
}
|
|
1288
|
-
if (isActiveReasoning) {
|
|
1289
|
-
controller.enqueue({
|
|
1290
|
-
type: "reasoning-end",
|
|
1291
|
-
id: "reasoning-0"
|
|
1292
|
-
});
|
|
1293
|
-
isActiveReasoning = false;
|
|
1294
|
-
}
|
|
1295
|
-
controller.enqueue({
|
|
1296
|
-
type: "text-delta",
|
|
1297
|
-
id: "txt-0",
|
|
1298
|
-
delta: delta.content
|
|
1299
|
-
});
|
|
1300
|
-
}
|
|
1301
|
-
if (delta.tool_calls != null && delta.tool_calls.length > 0) {
|
|
1302
|
-
if (isActiveReasoning) {
|
|
1303
|
-
controller.enqueue({
|
|
1304
|
-
type: "reasoning-end",
|
|
1305
|
-
id: "reasoning-0"
|
|
1306
|
-
});
|
|
1307
|
-
isActiveReasoning = false;
|
|
1308
|
-
}
|
|
1309
|
-
for (const [index, toolCallDelta] of delta.tool_calls.entries()) {
|
|
1310
|
-
const toolCallIndex = toolCallDelta.index ?? index;
|
|
1311
|
-
if (toolCallDelta.type != null) {
|
|
1312
|
-
toolCallTypes.set(toolCallIndex, toolCallDelta.type);
|
|
1313
|
-
}
|
|
1314
|
-
toolCallTracker.processDelta({
|
|
1315
|
-
...toolCallDelta,
|
|
1316
|
-
index: toolCallIndex
|
|
1317
|
-
});
|
|
1318
|
-
}
|
|
1319
|
-
}
|
|
1320
|
-
},
|
|
1321
|
-
flush(controller) {
|
|
1322
|
-
if (isActiveReasoning) {
|
|
1323
|
-
controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
|
|
1324
|
-
}
|
|
1325
|
-
if (isActiveText) {
|
|
1326
|
-
controller.enqueue({ type: "text-end", id: "txt-0" });
|
|
1327
|
-
}
|
|
1328
|
-
toolCallTracker.flush();
|
|
1329
|
-
controller.enqueue({
|
|
1330
|
-
type: "finish",
|
|
1331
|
-
finishReason,
|
|
1332
|
-
usage: convertMoonshotAIChatUsage(topLevelUsage ?? choiceUsage),
|
|
1333
|
-
providerMetadata: {
|
|
1334
|
-
[providerOptionsName]: {
|
|
1335
|
-
...contentLogprobs.length > 0 && {
|
|
1336
|
-
logprobs: { content: contentLogprobs }
|
|
1337
|
-
},
|
|
1338
|
-
...responseObject != null && { responseObject },
|
|
1339
|
-
...choiceIndex != null && { choiceIndex },
|
|
1340
|
-
...messageRole != null && { messageRole },
|
|
1341
|
-
...toolCallTypes.size > 0 && {
|
|
1342
|
-
toolCallTypes: [...toolCallTypes.entries()].sort(([left], [right]) => left - right).map(([, type]) => type)
|
|
1343
|
-
}
|
|
1344
|
-
}
|
|
1345
|
-
}
|
|
1346
|
-
});
|
|
1347
|
-
}
|
|
1348
|
-
})
|
|
1349
|
-
),
|
|
1350
|
-
request: { body },
|
|
1351
|
-
response: { headers: responseHeaders }
|
|
1352
|
-
};
|
|
1353
|
-
}
|
|
640
|
+
var MoonshotAIChatLanguageModel = class MoonshotAIChatLanguageModel {
|
|
641
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
642
|
+
return serializeModelOptions({
|
|
643
|
+
modelId: model.modelId,
|
|
644
|
+
config: model.config
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
648
|
+
return new MoonshotAIChatLanguageModel(options.modelId, options.config);
|
|
649
|
+
}
|
|
650
|
+
constructor(modelId, config) {
|
|
651
|
+
this.specificationVersion = "v4";
|
|
652
|
+
this.supportedUrls = {
|
|
653
|
+
"image/*": [/^ms:\/\//],
|
|
654
|
+
"video/*": [/^ms:\/\//]
|
|
655
|
+
};
|
|
656
|
+
this.modelId = modelId;
|
|
657
|
+
this.config = config;
|
|
658
|
+
this.failedResponseHandler = createJsonErrorResponseHandler({
|
|
659
|
+
errorSchema: moonshotAIErrorSchema,
|
|
660
|
+
errorToMessage: (error) => error.error.message
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
get provider() {
|
|
664
|
+
return this.config.provider;
|
|
665
|
+
}
|
|
666
|
+
get providerOptionsName() {
|
|
667
|
+
return this.config.provider.split(".")[0].trim();
|
|
668
|
+
}
|
|
669
|
+
async getArgs({ prompt, maxOutputTokens, temperature, topP, topK, frequencyPenalty, presencePenalty, reasoning, providerOptions, stopSequences, responseFormat, seed, toolChoice, tools }) {
|
|
670
|
+
const moonshotOptions = await parseProviderOptions({
|
|
671
|
+
provider: this.providerOptionsName,
|
|
672
|
+
providerOptions,
|
|
673
|
+
schema: moonshotaiLanguageModelOptions
|
|
674
|
+
}) ?? {};
|
|
675
|
+
const allWarnings = [];
|
|
676
|
+
if (topK != null) allWarnings.push({
|
|
677
|
+
type: "unsupported",
|
|
678
|
+
feature: "topK"
|
|
679
|
+
});
|
|
680
|
+
if (seed != null) allWarnings.push({
|
|
681
|
+
type: "unsupported",
|
|
682
|
+
feature: "seed"
|
|
683
|
+
});
|
|
684
|
+
const supportsSamplingOptions = !isMoonshotAIKimiModel(this.modelId);
|
|
685
|
+
if (!supportsSamplingOptions && temperature != null) allWarnings.push({
|
|
686
|
+
type: "unsupported",
|
|
687
|
+
feature: "temperature",
|
|
688
|
+
details: `temperature is fixed by model "${this.modelId}" and has been omitted.`
|
|
689
|
+
});
|
|
690
|
+
if (!supportsSamplingOptions && topP != null) allWarnings.push({
|
|
691
|
+
type: "unsupported",
|
|
692
|
+
feature: "topP",
|
|
693
|
+
details: `topP is fixed by model "${this.modelId}" and has been omitted.`
|
|
694
|
+
});
|
|
695
|
+
if (!supportsSamplingOptions && frequencyPenalty != null) allWarnings.push({
|
|
696
|
+
type: "unsupported",
|
|
697
|
+
feature: "frequencyPenalty",
|
|
698
|
+
details: `frequencyPenalty is fixed by model "${this.modelId}" and has been omitted.`
|
|
699
|
+
});
|
|
700
|
+
if (!supportsSamplingOptions && presencePenalty != null) allWarnings.push({
|
|
701
|
+
type: "unsupported",
|
|
702
|
+
feature: "presencePenalty",
|
|
703
|
+
details: `presencePenalty is fixed by model "${this.modelId}" and has been omitted.`
|
|
704
|
+
});
|
|
705
|
+
const { tools: moonshotTools, toolChoice: moonshotToolChoice, toolWarnings } = prepareTools({
|
|
706
|
+
tools,
|
|
707
|
+
toolChoice,
|
|
708
|
+
modelId: this.modelId
|
|
709
|
+
});
|
|
710
|
+
const modelFamily = getMoonshotAIModelFamily(this.modelId);
|
|
711
|
+
const requestedThinking = moonshotOptions.thinking;
|
|
712
|
+
const requestedReasoningEffort = moonshotOptions.reasoningEffort;
|
|
713
|
+
const preserveReasoning = moonshotOptions.reasoningHistory === "preserved";
|
|
714
|
+
if (requestedThinking?.budgetTokens != null) allWarnings.push({
|
|
715
|
+
type: "deprecated",
|
|
716
|
+
setting: "providerOptions.moonshotai.thinking.budgetTokens",
|
|
717
|
+
message: "Moonshot Chat Completions does not support budget_tokens. Remove budgetTokens; the option has been omitted."
|
|
718
|
+
});
|
|
719
|
+
let thinking;
|
|
720
|
+
let reasoningEffort;
|
|
721
|
+
const warnUnsupportedReasoningEffort = () => {
|
|
722
|
+
if (requestedReasoningEffort != null) allWarnings.push({
|
|
723
|
+
type: "unsupported",
|
|
724
|
+
feature: "reasoningEffort",
|
|
725
|
+
details: `reasoningEffort is only supported by Kimi K3 and has been omitted for model "${this.modelId}".`
|
|
726
|
+
});
|
|
727
|
+
};
|
|
728
|
+
switch (modelFamily) {
|
|
729
|
+
case "kimi-k3":
|
|
730
|
+
if (requestedThinking != null) allWarnings.push({
|
|
731
|
+
type: "unsupported",
|
|
732
|
+
feature: "thinking",
|
|
733
|
+
details: "Kimi K3 always reasons and does not accept the thinking field. The option has been omitted."
|
|
734
|
+
});
|
|
735
|
+
if (reasoning === "none") allWarnings.push({
|
|
736
|
+
type: "unsupported",
|
|
737
|
+
feature: "reasoning \"none\"",
|
|
738
|
+
details: "Kimi K3 reasoning cannot be disabled."
|
|
739
|
+
});
|
|
740
|
+
reasoningEffort = requestedReasoningEffort ?? (isCustomReasoning(reasoning) && reasoning !== "none" ? mapReasoningToProviderEffort({
|
|
741
|
+
reasoning,
|
|
742
|
+
effortMap: {
|
|
743
|
+
minimal: "low",
|
|
744
|
+
low: "low",
|
|
745
|
+
medium: "high",
|
|
746
|
+
high: "high",
|
|
747
|
+
xhigh: "max"
|
|
748
|
+
},
|
|
749
|
+
warnings: allWarnings
|
|
750
|
+
}) : void 0);
|
|
751
|
+
break;
|
|
752
|
+
case "kimi-k2.7":
|
|
753
|
+
warnUnsupportedReasoningEffort();
|
|
754
|
+
if (requestedThinking?.type === "disabled" || reasoning === "none") allWarnings.push({
|
|
755
|
+
type: "unsupported",
|
|
756
|
+
feature: requestedThinking?.type === "disabled" ? "thinking.type \"disabled\"" : "reasoning \"none\"",
|
|
757
|
+
details: "Kimi K2.7 thinking cannot be disabled."
|
|
758
|
+
});
|
|
759
|
+
else if (requestedThinking?.type === "enabled") thinking = { type: "enabled" };
|
|
760
|
+
break;
|
|
761
|
+
case "kimi-k2.6": {
|
|
762
|
+
warnUnsupportedReasoningEffort();
|
|
763
|
+
const thinkingType = requestedThinking?.type ?? (isCustomReasoning(reasoning) ? reasoning === "none" ? "disabled" : "enabled" : void 0);
|
|
764
|
+
if (thinkingType != null || preserveReasoning) thinking = {
|
|
765
|
+
type: thinkingType ?? "enabled",
|
|
766
|
+
...preserveReasoning ? { keep: "all" } : {}
|
|
767
|
+
};
|
|
768
|
+
break;
|
|
769
|
+
}
|
|
770
|
+
case "kimi-k2.5": {
|
|
771
|
+
warnUnsupportedReasoningEffort();
|
|
772
|
+
const thinkingType = requestedThinking?.type ?? (isCustomReasoning(reasoning) ? reasoning === "none" ? "disabled" : "enabled" : void 0);
|
|
773
|
+
if (thinkingType != null) thinking = { type: thinkingType };
|
|
774
|
+
if (preserveReasoning) allWarnings.push({
|
|
775
|
+
type: "unsupported",
|
|
776
|
+
feature: `reasoningHistory 'preserved' is not supported by model "${this.modelId}"`
|
|
777
|
+
});
|
|
778
|
+
break;
|
|
779
|
+
}
|
|
780
|
+
case "moonshot-v1":
|
|
781
|
+
warnUnsupportedReasoningEffort();
|
|
782
|
+
if (requestedThinking != null) allWarnings.push({
|
|
783
|
+
type: "unsupported",
|
|
784
|
+
feature: "thinking",
|
|
785
|
+
details: `thinking is not supported by model "${this.modelId}" and has been omitted.`
|
|
786
|
+
});
|
|
787
|
+
if (isCustomReasoning(reasoning) && reasoning !== "none") allWarnings.push({
|
|
788
|
+
type: "unsupported",
|
|
789
|
+
feature: "reasoning",
|
|
790
|
+
details: `reasoning is not supported by model "${this.modelId}".`
|
|
791
|
+
});
|
|
792
|
+
if (preserveReasoning) allWarnings.push({
|
|
793
|
+
type: "unsupported",
|
|
794
|
+
feature: `reasoningHistory 'preserved' is not supported by model "${this.modelId}"`
|
|
795
|
+
});
|
|
796
|
+
break;
|
|
797
|
+
case "unknown":
|
|
798
|
+
if (reasoning === "none") allWarnings.push({
|
|
799
|
+
type: "unsupported",
|
|
800
|
+
feature: "reasoning \"none\"",
|
|
801
|
+
details: "Use providerOptions.moonshotai.thinking to control thinking on custom models."
|
|
802
|
+
});
|
|
803
|
+
reasoningEffort = requestedReasoningEffort ?? (isCustomReasoning(reasoning) && reasoning !== "none" ? mapReasoningToProviderEffort({
|
|
804
|
+
reasoning,
|
|
805
|
+
effortMap: {
|
|
806
|
+
minimal: "low",
|
|
807
|
+
low: "low",
|
|
808
|
+
medium: "high",
|
|
809
|
+
high: "high",
|
|
810
|
+
xhigh: "max"
|
|
811
|
+
},
|
|
812
|
+
warnings: allWarnings
|
|
813
|
+
}) : void 0);
|
|
814
|
+
if (requestedThinking?.type != null) thinking = { type: requestedThinking.type };
|
|
815
|
+
if (preserveReasoning) allWarnings.push({
|
|
816
|
+
type: "unsupported",
|
|
817
|
+
feature: `reasoningHistory 'preserved' is not supported by model "${this.modelId}"`
|
|
818
|
+
});
|
|
819
|
+
}
|
|
820
|
+
let response_format;
|
|
821
|
+
if (responseFormat?.type === "json") {
|
|
822
|
+
if (this.config.supportsStructuredOutputs === true && responseFormat.schema != null) {
|
|
823
|
+
const { $schema: _$schema, ...schemaWithoutDollarSchema } = responseFormat.schema;
|
|
824
|
+
response_format = {
|
|
825
|
+
type: "json_schema",
|
|
826
|
+
json_schema: {
|
|
827
|
+
name: responseFormat.name ?? "response",
|
|
828
|
+
strict: moonshotOptions.strictJsonSchema ?? true,
|
|
829
|
+
schema: normalizeJsonSchemaForMFJS(schemaWithoutDollarSchema)
|
|
830
|
+
}
|
|
831
|
+
};
|
|
832
|
+
} else response_format = { type: "json_object" };
|
|
833
|
+
}
|
|
834
|
+
const { messages, warnings: messageWarnings } = await convertToMoonshotAIChatMessages({
|
|
835
|
+
modelId: this.modelId,
|
|
836
|
+
prompt,
|
|
837
|
+
providerOptionsName: this.providerOptionsName,
|
|
838
|
+
responseFormat: response_format
|
|
839
|
+
});
|
|
840
|
+
allWarnings.push(...messageWarnings);
|
|
841
|
+
return {
|
|
842
|
+
args: {
|
|
843
|
+
model: this.modelId,
|
|
844
|
+
...(moonshotOptions.logprobs === true || moonshotOptions.topLogprobs != null) && { logprobs: true },
|
|
845
|
+
...moonshotOptions.topLogprobs != null && { top_logprobs: moonshotOptions.topLogprobs },
|
|
846
|
+
max_completion_tokens: maxOutputTokens,
|
|
847
|
+
temperature: supportsSamplingOptions ? temperature : void 0,
|
|
848
|
+
top_p: supportsSamplingOptions ? topP : void 0,
|
|
849
|
+
frequency_penalty: supportsSamplingOptions ? frequencyPenalty : void 0,
|
|
850
|
+
presence_penalty: supportsSamplingOptions ? presencePenalty : void 0,
|
|
851
|
+
response_format,
|
|
852
|
+
stop: stopSequences,
|
|
853
|
+
messages,
|
|
854
|
+
tools: moonshotTools,
|
|
855
|
+
tool_choice: moonshotToolChoice,
|
|
856
|
+
...moonshotOptions.prediction != null && { prediction: moonshotOptions.prediction },
|
|
857
|
+
...thinking != null ? { thinking } : {},
|
|
858
|
+
...reasoningEffort != null && { reasoning_effort: reasoningEffort },
|
|
859
|
+
...moonshotOptions.promptCacheKey != null && { prompt_cache_key: moonshotOptions.promptCacheKey },
|
|
860
|
+
...moonshotOptions.safetyIdentifier != null && { safety_identifier: moonshotOptions.safetyIdentifier }
|
|
861
|
+
},
|
|
862
|
+
warnings: [...allWarnings, ...toolWarnings]
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
async doGenerate(options) {
|
|
866
|
+
const { args, warnings } = await this.getArgs({ ...options });
|
|
867
|
+
const { responseHeaders, value: responseBody, rawValue: rawResponse } = await postJsonToApi({
|
|
868
|
+
url: this.config.url({
|
|
869
|
+
path: "/chat/completions",
|
|
870
|
+
modelId: this.modelId
|
|
871
|
+
}),
|
|
872
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
873
|
+
body: args,
|
|
874
|
+
failedResponseHandler: this.failedResponseHandler,
|
|
875
|
+
successfulResponseHandler: createJsonResponseHandler(moonshotAIChatResponseSchema),
|
|
876
|
+
abortSignal: options.abortSignal,
|
|
877
|
+
fetch: this.config.fetch
|
|
878
|
+
});
|
|
879
|
+
const choice = responseBody.choices[0];
|
|
880
|
+
const content = [];
|
|
881
|
+
const reasoning = choice.message.reasoning_content;
|
|
882
|
+
if (reasoning != null && reasoning.length > 0) content.push({
|
|
883
|
+
type: "reasoning",
|
|
884
|
+
text: reasoning
|
|
885
|
+
});
|
|
886
|
+
if (choice.message.tool_calls != null) for (const toolCall of choice.message.tool_calls) content.push({
|
|
887
|
+
type: "tool-call",
|
|
888
|
+
toolCallId: toolCall.id || generateId(),
|
|
889
|
+
toolName: toolCall.function.name,
|
|
890
|
+
input: toolCall.function.arguments ?? ""
|
|
891
|
+
});
|
|
892
|
+
const text = choice.message.content;
|
|
893
|
+
if (text != null && text.length > 0) content.push({
|
|
894
|
+
type: "text",
|
|
895
|
+
text
|
|
896
|
+
});
|
|
897
|
+
return {
|
|
898
|
+
content,
|
|
899
|
+
finishReason: {
|
|
900
|
+
unified: mapMoonshotAIFinishReason(choice.finish_reason),
|
|
901
|
+
raw: choice.finish_reason ?? void 0
|
|
902
|
+
},
|
|
903
|
+
usage: convertMoonshotAIChatUsage(responseBody.usage),
|
|
904
|
+
providerMetadata: { [this.providerOptionsName]: {
|
|
905
|
+
...choice.logprobs != null && { logprobs: choice.logprobs },
|
|
906
|
+
...responseBody.object != null && { responseObject: responseBody.object },
|
|
907
|
+
...choice.index != null && { choiceIndex: choice.index },
|
|
908
|
+
...choice.message.role != null && { messageRole: choice.message.role },
|
|
909
|
+
...choice.message.tool_calls != null && { toolCallTypes: choice.message.tool_calls.map((toolCall) => toolCall.type).filter((type) => type != null) }
|
|
910
|
+
} },
|
|
911
|
+
request: { body: args },
|
|
912
|
+
response: {
|
|
913
|
+
...createLanguageModelResponseMetadata(responseBody),
|
|
914
|
+
headers: responseHeaders,
|
|
915
|
+
body: rawResponse
|
|
916
|
+
},
|
|
917
|
+
warnings
|
|
918
|
+
};
|
|
919
|
+
}
|
|
920
|
+
async doStream(options) {
|
|
921
|
+
const { args, warnings } = await this.getArgs({ ...options });
|
|
922
|
+
const body = {
|
|
923
|
+
...args,
|
|
924
|
+
stream: true,
|
|
925
|
+
...this.config.includeUsage && { stream_options: { include_usage: true } }
|
|
926
|
+
};
|
|
927
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
928
|
+
url: this.config.url({
|
|
929
|
+
path: "/chat/completions",
|
|
930
|
+
modelId: this.modelId
|
|
931
|
+
}),
|
|
932
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
933
|
+
body,
|
|
934
|
+
failedResponseHandler: this.failedResponseHandler,
|
|
935
|
+
successfulResponseHandler: createEventSourceResponseHandler(moonshotAIChatChunkSchema),
|
|
936
|
+
abortSignal: options.abortSignal,
|
|
937
|
+
fetch: this.config.fetch
|
|
938
|
+
});
|
|
939
|
+
let toolCallTracker;
|
|
940
|
+
let finishReason = {
|
|
941
|
+
unified: "other",
|
|
942
|
+
raw: void 0
|
|
943
|
+
};
|
|
944
|
+
let topLevelUsage = void 0;
|
|
945
|
+
let choiceUsage = void 0;
|
|
946
|
+
const contentLogprobs = [];
|
|
947
|
+
const providerOptionsName = this.providerOptionsName;
|
|
948
|
+
let isFirstChunk = true;
|
|
949
|
+
let isActiveReasoning = false;
|
|
950
|
+
let isActiveText = false;
|
|
951
|
+
let responseObject;
|
|
952
|
+
let choiceIndex;
|
|
953
|
+
let messageRole;
|
|
954
|
+
const toolCallTypes = /* @__PURE__ */ new Map();
|
|
955
|
+
return {
|
|
956
|
+
stream: response.pipeThrough(new TransformStream({
|
|
957
|
+
start(controller) {
|
|
958
|
+
toolCallTracker = new StreamingToolCallTracker(controller, { generateId });
|
|
959
|
+
controller.enqueue({
|
|
960
|
+
type: "stream-start",
|
|
961
|
+
warnings
|
|
962
|
+
});
|
|
963
|
+
},
|
|
964
|
+
transform(chunk, controller) {
|
|
965
|
+
if (options.includeRawChunks) controller.enqueue({
|
|
966
|
+
type: "raw",
|
|
967
|
+
rawValue: chunk.rawValue
|
|
968
|
+
});
|
|
969
|
+
if (!chunk.success) {
|
|
970
|
+
finishReason = {
|
|
971
|
+
unified: "error",
|
|
972
|
+
raw: void 0
|
|
973
|
+
};
|
|
974
|
+
controller.enqueue({
|
|
975
|
+
type: "error",
|
|
976
|
+
error: chunk.error
|
|
977
|
+
});
|
|
978
|
+
return;
|
|
979
|
+
}
|
|
980
|
+
const value = chunk.value;
|
|
981
|
+
if ("error" in value) {
|
|
982
|
+
finishReason = {
|
|
983
|
+
unified: "error",
|
|
984
|
+
raw: void 0
|
|
985
|
+
};
|
|
986
|
+
controller.enqueue({
|
|
987
|
+
type: "error",
|
|
988
|
+
error: createMoonshotAIStreamError(value.error, value)
|
|
989
|
+
});
|
|
990
|
+
return;
|
|
991
|
+
}
|
|
992
|
+
if (isFirstChunk) {
|
|
993
|
+
isFirstChunk = false;
|
|
994
|
+
controller.enqueue({
|
|
995
|
+
type: "response-metadata",
|
|
996
|
+
...createLanguageModelResponseMetadata(value)
|
|
997
|
+
});
|
|
998
|
+
}
|
|
999
|
+
if (value.usage != null) topLevelUsage = value.usage;
|
|
1000
|
+
if (value.object != null) responseObject = value.object;
|
|
1001
|
+
const choice = value.choices[0];
|
|
1002
|
+
if (choice?.usage != null) choiceUsage = choice.usage;
|
|
1003
|
+
if (choice?.index != null) choiceIndex = choice.index;
|
|
1004
|
+
if (choice?.finish_reason != null) finishReason = {
|
|
1005
|
+
unified: mapMoonshotAIFinishReason(choice.finish_reason),
|
|
1006
|
+
raw: choice.finish_reason
|
|
1007
|
+
};
|
|
1008
|
+
if (choice?.logprobs?.content != null) contentLogprobs.push(...choice.logprobs.content);
|
|
1009
|
+
if (choice?.delta == null) return;
|
|
1010
|
+
const delta = choice.delta;
|
|
1011
|
+
if (delta.role != null) messageRole = delta.role;
|
|
1012
|
+
const reasoningContent = delta.reasoning_content;
|
|
1013
|
+
if (reasoningContent) {
|
|
1014
|
+
if (!isActiveReasoning) {
|
|
1015
|
+
controller.enqueue({
|
|
1016
|
+
type: "reasoning-start",
|
|
1017
|
+
id: "reasoning-0"
|
|
1018
|
+
});
|
|
1019
|
+
isActiveReasoning = true;
|
|
1020
|
+
}
|
|
1021
|
+
controller.enqueue({
|
|
1022
|
+
type: "reasoning-delta",
|
|
1023
|
+
id: "reasoning-0",
|
|
1024
|
+
delta: reasoningContent
|
|
1025
|
+
});
|
|
1026
|
+
}
|
|
1027
|
+
if (delta.content) {
|
|
1028
|
+
if (!isActiveText) {
|
|
1029
|
+
controller.enqueue({
|
|
1030
|
+
type: "text-start",
|
|
1031
|
+
id: "txt-0"
|
|
1032
|
+
});
|
|
1033
|
+
isActiveText = true;
|
|
1034
|
+
}
|
|
1035
|
+
if (isActiveReasoning) {
|
|
1036
|
+
controller.enqueue({
|
|
1037
|
+
type: "reasoning-end",
|
|
1038
|
+
id: "reasoning-0"
|
|
1039
|
+
});
|
|
1040
|
+
isActiveReasoning = false;
|
|
1041
|
+
}
|
|
1042
|
+
controller.enqueue({
|
|
1043
|
+
type: "text-delta",
|
|
1044
|
+
id: "txt-0",
|
|
1045
|
+
delta: delta.content
|
|
1046
|
+
});
|
|
1047
|
+
}
|
|
1048
|
+
if (delta.tool_calls != null && delta.tool_calls.length > 0) {
|
|
1049
|
+
if (isActiveReasoning) {
|
|
1050
|
+
controller.enqueue({
|
|
1051
|
+
type: "reasoning-end",
|
|
1052
|
+
id: "reasoning-0"
|
|
1053
|
+
});
|
|
1054
|
+
isActiveReasoning = false;
|
|
1055
|
+
}
|
|
1056
|
+
for (const [index, toolCallDelta] of delta.tool_calls.entries()) {
|
|
1057
|
+
const toolCallIndex = toolCallDelta.index ?? index;
|
|
1058
|
+
if (toolCallDelta.type != null) toolCallTypes.set(toolCallIndex, toolCallDelta.type);
|
|
1059
|
+
toolCallTracker.processDelta({
|
|
1060
|
+
...toolCallDelta,
|
|
1061
|
+
index: toolCallIndex
|
|
1062
|
+
});
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
},
|
|
1066
|
+
flush(controller) {
|
|
1067
|
+
if (isActiveReasoning) controller.enqueue({
|
|
1068
|
+
type: "reasoning-end",
|
|
1069
|
+
id: "reasoning-0"
|
|
1070
|
+
});
|
|
1071
|
+
if (isActiveText) controller.enqueue({
|
|
1072
|
+
type: "text-end",
|
|
1073
|
+
id: "txt-0"
|
|
1074
|
+
});
|
|
1075
|
+
toolCallTracker.flush();
|
|
1076
|
+
controller.enqueue({
|
|
1077
|
+
type: "finish",
|
|
1078
|
+
finishReason,
|
|
1079
|
+
usage: convertMoonshotAIChatUsage(topLevelUsage ?? choiceUsage),
|
|
1080
|
+
providerMetadata: { [providerOptionsName]: {
|
|
1081
|
+
...contentLogprobs.length > 0 && { logprobs: { content: contentLogprobs } },
|
|
1082
|
+
...responseObject != null && { responseObject },
|
|
1083
|
+
...choiceIndex != null && { choiceIndex },
|
|
1084
|
+
...messageRole != null && { messageRole },
|
|
1085
|
+
...toolCallTypes.size > 0 && { toolCallTypes: [...toolCallTypes.entries()].sort(([left], [right]) => left - right).map(([, type]) => type) }
|
|
1086
|
+
} }
|
|
1087
|
+
});
|
|
1088
|
+
}
|
|
1089
|
+
})),
|
|
1090
|
+
request: { body },
|
|
1091
|
+
response: { headers: responseHeaders }
|
|
1092
|
+
};
|
|
1093
|
+
}
|
|
1354
1094
|
};
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1095
|
+
//#endregion
|
|
1096
|
+
//#region src/version.ts
|
|
1097
|
+
const VERSION = "3.0.61";
|
|
1098
|
+
//#endregion
|
|
1099
|
+
//#region src/moonshotai-provider.ts
|
|
1100
|
+
const defaultBaseURL = "https://api.moonshot.ai/v1";
|
|
1361
1101
|
function getModelStructuredOutputSupport(modelId) {
|
|
1362
|
-
|
|
1102
|
+
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";
|
|
1363
1103
|
}
|
|
1364
1104
|
function createMoonshotAI(options = {}) {
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1105
|
+
const baseURL = withoutTrailingSlash(options.baseURL ?? defaultBaseURL);
|
|
1106
|
+
const getHeaders = () => withUserAgentSuffix({
|
|
1107
|
+
Authorization: `Bearer ${loadApiKey({
|
|
1108
|
+
apiKey: options.apiKey,
|
|
1109
|
+
environmentVariableName: "MOONSHOT_API_KEY",
|
|
1110
|
+
description: "Moonshot API key"
|
|
1111
|
+
})}`,
|
|
1112
|
+
...options.headers
|
|
1113
|
+
}, `ai-sdk-moonshotai/${VERSION}`);
|
|
1114
|
+
const createChatModel = (modelId) => {
|
|
1115
|
+
return new MoonshotAIChatLanguageModel(modelId, {
|
|
1116
|
+
provider: "moonshotai.chat",
|
|
1117
|
+
url: ({ path }) => `${baseURL}${path}`,
|
|
1118
|
+
headers: getHeaders,
|
|
1119
|
+
fetch: options.fetch,
|
|
1120
|
+
includeUsage: true,
|
|
1121
|
+
supportsStructuredOutputs: getModelStructuredOutputSupport(modelId)
|
|
1122
|
+
});
|
|
1123
|
+
};
|
|
1124
|
+
const provider = (modelId) => createChatModel(modelId);
|
|
1125
|
+
provider.specificationVersion = "v4";
|
|
1126
|
+
provider.chatModel = createChatModel;
|
|
1127
|
+
provider.languageModel = createChatModel;
|
|
1128
|
+
provider.embeddingModel = (modelId) => {
|
|
1129
|
+
throw new NoSuchModelError({
|
|
1130
|
+
modelId,
|
|
1131
|
+
modelType: "embeddingModel"
|
|
1132
|
+
});
|
|
1133
|
+
};
|
|
1134
|
+
provider.imageModel = (modelId) => {
|
|
1135
|
+
throw new NoSuchModelError({
|
|
1136
|
+
modelId,
|
|
1137
|
+
modelType: "imageModel"
|
|
1138
|
+
});
|
|
1139
|
+
};
|
|
1140
|
+
return provider;
|
|
1398
1141
|
}
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
moonshotai
|
|
1404
|
-
};
|
|
1142
|
+
const moonshotai = createMoonshotAI();
|
|
1143
|
+
//#endregion
|
|
1144
|
+
export { VERSION, createMoonshotAI, moonshotai };
|
|
1145
|
+
|
|
1405
1146
|
//# sourceMappingURL=index.js.map
|