@ai-sdk/moonshotai 3.0.38 → 3.0.42
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 +47 -0
- package/README.md +44 -11
- package/dist/index.d.ts +61 -24
- package/dist/index.js +819 -290
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/convert-to-moonshotai-chat-messages.ts +229 -19
- package/src/index.ts +3 -0
- package/src/moonshotai-chat-api-types.ts +58 -7
- package/src/moonshotai-chat-language-model.ts +356 -70
- package/src/moonshotai-chat-options.ts +181 -17
- package/src/moonshotai-prepare-tools.ts +24 -20
- package/src/moonshotai-provider.ts +10 -2
|
@@ -1,9 +1,14 @@
|
|
|
1
|
+
import type { LanguageModelV4FunctionTool } from '@ai-sdk/provider';
|
|
1
2
|
import { z } from 'zod/v4';
|
|
2
3
|
|
|
3
4
|
export type MoonshotAIChatModelId =
|
|
5
|
+
| 'moonshot-v1-auto'
|
|
4
6
|
| 'moonshot-v1-8k'
|
|
5
7
|
| 'moonshot-v1-32k'
|
|
6
8
|
| 'moonshot-v1-128k'
|
|
9
|
+
| 'moonshot-v1-8k-vision-preview'
|
|
10
|
+
| 'moonshot-v1-32k-vision-preview'
|
|
11
|
+
| 'moonshot-v1-128k-vision-preview'
|
|
7
12
|
| 'kimi-k2.5'
|
|
8
13
|
| 'kimi-k2.6'
|
|
9
14
|
| 'kimi-k2.7-code'
|
|
@@ -11,19 +16,94 @@ export type MoonshotAIChatModelId =
|
|
|
11
16
|
| 'kimi-k3'
|
|
12
17
|
| (string & {});
|
|
13
18
|
|
|
19
|
+
export function isMoonshotAIKimiModel(modelId: MoonshotAIChatModelId): boolean {
|
|
20
|
+
return getMoonshotAIModelFamily(modelId).startsWith('kimi-');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type MoonshotAIModelFamily =
|
|
24
|
+
| 'kimi-k2.5'
|
|
25
|
+
| 'kimi-k2.6'
|
|
26
|
+
| 'kimi-k2.7'
|
|
27
|
+
| 'kimi-k3'
|
|
28
|
+
| 'moonshot-v1'
|
|
29
|
+
| 'unknown';
|
|
30
|
+
|
|
31
|
+
export function getMoonshotAIModelFamily(
|
|
32
|
+
modelId: MoonshotAIChatModelId,
|
|
33
|
+
): MoonshotAIModelFamily {
|
|
34
|
+
if (modelId === 'kimi-k2.5') return 'kimi-k2.5';
|
|
35
|
+
if (modelId === 'kimi-k2.6') return 'kimi-k2.6';
|
|
36
|
+
if (modelId === 'kimi-k2.7-code' || modelId === 'kimi-k2.7-code-highspeed') {
|
|
37
|
+
return 'kimi-k2.7';
|
|
38
|
+
}
|
|
39
|
+
if (modelId === 'kimi-k3') return 'kimi-k3';
|
|
40
|
+
if (modelId.startsWith('moonshot-v1-')) return 'moonshot-v1';
|
|
41
|
+
return 'unknown';
|
|
42
|
+
}
|
|
43
|
+
|
|
14
44
|
export const moonshotaiLanguageModelOptions = z.object({
|
|
15
45
|
/**
|
|
16
|
-
*
|
|
46
|
+
* Whether to use strict JSON schema validation for structured outputs.
|
|
47
|
+
*
|
|
48
|
+
* @default true
|
|
49
|
+
*/
|
|
50
|
+
strictJsonSchema: z.boolean().optional(),
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Whether to return log probabilities for generated tokens.
|
|
54
|
+
*/
|
|
55
|
+
logprobs: z.boolean().optional(),
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Number of most likely tokens to return at each token position.
|
|
59
|
+
*
|
|
60
|
+
* Setting this option automatically enables `logprobs`.
|
|
61
|
+
*/
|
|
62
|
+
topLogprobs: z.number().int().min(0).max(20).optional(),
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Reasoning effort for Kimi K3. Supports `low`, `high`, and `max`;
|
|
66
|
+
* defaults to `max`.
|
|
17
67
|
*/
|
|
18
68
|
reasoningEffort: z.enum(['low', 'high', 'max']).optional(),
|
|
19
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Static predicted content that can accelerate responses when much of the
|
|
72
|
+
* output is known ahead of time.
|
|
73
|
+
*/
|
|
74
|
+
prediction: z
|
|
75
|
+
.object({
|
|
76
|
+
type: z.literal('content'),
|
|
77
|
+
content: z.union([
|
|
78
|
+
z.string(),
|
|
79
|
+
z.array(z.object({ type: z.literal('text'), text: z.string() })),
|
|
80
|
+
]),
|
|
81
|
+
})
|
|
82
|
+
.optional(),
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Thinking configuration for Kimi K2.x models. Kimi K2.5 and K2.6 support
|
|
86
|
+
* enabling or disabling thinking. Kimi K2.7 Code always has thinking
|
|
87
|
+
* enabled.
|
|
88
|
+
*/
|
|
20
89
|
thinking: z
|
|
21
90
|
.object({
|
|
22
91
|
type: z.enum(['enabled', 'disabled']).optional(),
|
|
92
|
+
/**
|
|
93
|
+
* @deprecated Moonshot Chat Completions does not support thinking
|
|
94
|
+
* budgets. Accepted for backwards compatibility, then omitted with a
|
|
95
|
+
* warning.
|
|
96
|
+
*/
|
|
23
97
|
budgetTokens: z.number().int().min(1024).optional(),
|
|
24
98
|
})
|
|
25
99
|
.optional(),
|
|
26
100
|
|
|
101
|
+
/**
|
|
102
|
+
* Controls preserved reasoning behavior in multi-turn conversations.
|
|
103
|
+
* `disabled` and `interleaved` are compatibility values that leave the
|
|
104
|
+
* request unchanged. `preserved` maps to `thinking.keep: 'all'` for Kimi
|
|
105
|
+
* K2.6. Kimi K2.7 and K3 preserve reasoning by default.
|
|
106
|
+
*/
|
|
27
107
|
reasoningHistory: z.enum(['disabled', 'interleaved', 'preserved']).optional(),
|
|
28
108
|
|
|
29
109
|
/**
|
|
@@ -39,21 +119,105 @@ export const moonshotaiLanguageModelOptions = z.object({
|
|
|
39
119
|
safetyIdentifier: z.string().optional(),
|
|
40
120
|
});
|
|
41
121
|
|
|
42
|
-
export type MoonshotAILanguageModelOptions =
|
|
43
|
-
|
|
122
|
+
export type MoonshotAILanguageModelOptions = {
|
|
123
|
+
/**
|
|
124
|
+
* Whether to use strict JSON schema validation for structured outputs.
|
|
125
|
+
*
|
|
126
|
+
* @default true
|
|
127
|
+
*/
|
|
128
|
+
strictJsonSchema?: boolean;
|
|
129
|
+
|
|
130
|
+
/** Whether to return log probabilities for generated tokens. */
|
|
131
|
+
logprobs?: boolean;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Number of most likely tokens to return at each token position.
|
|
135
|
+
* Setting this option automatically enables `logprobs`.
|
|
136
|
+
*/
|
|
137
|
+
topLogprobs?: number;
|
|
138
|
+
|
|
139
|
+
/** Reasoning effort for Kimi K3. */
|
|
140
|
+
reasoningEffort?: 'low' | 'high' | 'max';
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Static predicted content that can accelerate responses when much of the
|
|
144
|
+
* output is known ahead of time.
|
|
145
|
+
*/
|
|
146
|
+
prediction?: {
|
|
147
|
+
type: 'content';
|
|
148
|
+
content: string | Array<{ type: 'text'; text: string }>;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/** Controls thinking on Kimi K2.5 and K2.6. K2.7 is always enabled. */
|
|
152
|
+
thinking?: {
|
|
153
|
+
type?: 'enabled' | 'disabled';
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @deprecated Moonshot Chat Completions does not support thinking budgets.
|
|
157
|
+
* This value is ignored with a warning.
|
|
158
|
+
*/
|
|
159
|
+
budgetTokens?: number;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Controls preserved reasoning behavior in multi-turn conversations.
|
|
164
|
+
* `disabled` and `interleaved` are compatibility values that leave the
|
|
165
|
+
* request unchanged. `preserved` maps to `thinking.keep: 'all'` for Kimi
|
|
166
|
+
* K2.6. Kimi K2.7 and K3 preserve reasoning by default.
|
|
167
|
+
*/
|
|
168
|
+
reasoningHistory?: 'disabled' | 'interleaved' | 'preserved';
|
|
169
|
+
promptCacheKey?: string;
|
|
170
|
+
safetyIdentifier?: string;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
export const moonshotaiMessageProviderOptions = z.object({
|
|
174
|
+
/**
|
|
175
|
+
* The name of the participant represented by the message.
|
|
176
|
+
*
|
|
177
|
+
* Supported on system, user, and assistant messages.
|
|
178
|
+
*/
|
|
179
|
+
name: z.string().optional(),
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
export type MoonshotAIMessageProviderOptions = z.infer<
|
|
183
|
+
typeof moonshotaiMessageProviderOptions
|
|
44
184
|
>;
|
|
45
185
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
186
|
+
export const moonshotaiAssistantMessageProviderOptions =
|
|
187
|
+
moonshotaiMessageProviderOptions.extend({
|
|
188
|
+
/**
|
|
189
|
+
* Whether the assistant message content is a partial response that Moonshot
|
|
190
|
+
* should continue. Only supported on the final assistant message and cannot
|
|
191
|
+
* be combined with JSON object response format.
|
|
192
|
+
*/
|
|
193
|
+
partial: z.literal(true).optional(),
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
export type MoonshotAIAssistantMessageProviderOptions = z.infer<
|
|
197
|
+
typeof moonshotaiAssistantMessageProviderOptions
|
|
198
|
+
>;
|
|
199
|
+
|
|
200
|
+
const moonshotaiDynamicToolSchema = z.object({
|
|
201
|
+
type: z.literal('function'),
|
|
202
|
+
name: z.string(),
|
|
203
|
+
description: z.string().optional(),
|
|
204
|
+
inputSchema: z.record(z.string(), z.unknown()),
|
|
205
|
+
strict: z.boolean().optional(),
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
export const moonshotaiAllMessageProviderOptions =
|
|
209
|
+
moonshotaiAssistantMessageProviderOptions.extend({
|
|
210
|
+
/** Function tools to load at this point in a Kimi K3 conversation. */
|
|
211
|
+
tools: z.array(moonshotaiDynamicToolSchema).optional(),
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
export type MoonshotAISystemMessageProviderOptions =
|
|
215
|
+
MoonshotAIMessageProviderOptions & {
|
|
216
|
+
/** Function tools to load at this point in a Kimi K3 conversation. */
|
|
217
|
+
tools?: Array<
|
|
218
|
+
Pick<
|
|
219
|
+
LanguageModelV4FunctionTool,
|
|
220
|
+
'type' | 'name' | 'description' | 'inputSchema' | 'strict'
|
|
221
|
+
>
|
|
222
|
+
>;
|
|
223
|
+
};
|
|
@@ -3,26 +3,20 @@ import {
|
|
|
3
3
|
type LanguageModelV4CallOptions,
|
|
4
4
|
type SharedV4Warning,
|
|
5
5
|
} from '@ai-sdk/provider';
|
|
6
|
+
import type { MoonshotAIFunctionTool } from './moonshotai-chat-api-types';
|
|
7
|
+
import type { MoonshotAIChatModelId } from './moonshotai-chat-options';
|
|
6
8
|
import { normalizeJsonSchemaForMFJS } from './normalize-json-schema-for-mfjs';
|
|
7
9
|
|
|
8
10
|
export function prepareTools({
|
|
9
11
|
tools,
|
|
10
12
|
toolChoice,
|
|
13
|
+
modelId,
|
|
11
14
|
}: {
|
|
12
15
|
tools: LanguageModelV4CallOptions['tools'];
|
|
13
16
|
toolChoice?: LanguageModelV4CallOptions['toolChoice'];
|
|
17
|
+
modelId: MoonshotAIChatModelId;
|
|
14
18
|
}): {
|
|
15
|
-
tools:
|
|
16
|
-
| undefined
|
|
17
|
-
| Array<{
|
|
18
|
-
type: 'function';
|
|
19
|
-
function: {
|
|
20
|
-
name: string;
|
|
21
|
-
description: string | undefined;
|
|
22
|
-
parameters: unknown;
|
|
23
|
-
strict?: boolean;
|
|
24
|
-
};
|
|
25
|
-
}>;
|
|
19
|
+
tools: undefined | Array<MoonshotAIFunctionTool>;
|
|
26
20
|
toolChoice:
|
|
27
21
|
| { type: 'function'; function: { name: string } }
|
|
28
22
|
| 'auto'
|
|
@@ -40,15 +34,7 @@ export function prepareTools({
|
|
|
40
34
|
return { tools: undefined, toolChoice: undefined, toolWarnings };
|
|
41
35
|
}
|
|
42
36
|
|
|
43
|
-
const moonshotTools: Array<
|
|
44
|
-
type: 'function';
|
|
45
|
-
function: {
|
|
46
|
-
name: string;
|
|
47
|
-
description: string | undefined;
|
|
48
|
-
parameters: unknown;
|
|
49
|
-
strict?: boolean;
|
|
50
|
-
};
|
|
51
|
-
}> = [];
|
|
37
|
+
const moonshotTools: Array<MoonshotAIFunctionTool> = [];
|
|
52
38
|
|
|
53
39
|
for (const tool of tools) {
|
|
54
40
|
if (tool.type === 'provider') {
|
|
@@ -78,7 +64,25 @@ export function prepareTools({
|
|
|
78
64
|
switch (type) {
|
|
79
65
|
case 'auto':
|
|
80
66
|
case 'none':
|
|
67
|
+
return { tools: moonshotTools, toolChoice: type, toolWarnings };
|
|
81
68
|
case 'required':
|
|
69
|
+
if (
|
|
70
|
+
modelId === 'kimi-k2.6' ||
|
|
71
|
+
modelId === 'kimi-k2.7-code' ||
|
|
72
|
+
modelId === 'kimi-k2.7-code-highspeed'
|
|
73
|
+
) {
|
|
74
|
+
toolWarnings.push({
|
|
75
|
+
type: 'unsupported',
|
|
76
|
+
feature: `tool choice "required" for model "${modelId}"`,
|
|
77
|
+
details:
|
|
78
|
+
'Moonshot AI rejects required tool choice for this model. The setting has been omitted; use "auto" or select a specific tool instead.',
|
|
79
|
+
});
|
|
80
|
+
return {
|
|
81
|
+
tools: moonshotTools,
|
|
82
|
+
toolChoice: undefined,
|
|
83
|
+
toolWarnings,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
82
86
|
return { tools: moonshotTools, toolChoice: type, toolWarnings };
|
|
83
87
|
case 'tool':
|
|
84
88
|
return {
|
|
@@ -58,8 +58,16 @@ const defaultBaseURL = 'https://api.moonshot.ai/v1';
|
|
|
58
58
|
export function getModelStructuredOutputSupport(
|
|
59
59
|
modelId: MoonshotAIChatModelId,
|
|
60
60
|
): boolean {
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
return (
|
|
62
|
+
modelId.startsWith('kimi-k') ||
|
|
63
|
+
modelId === 'moonshot-v1-8k' ||
|
|
64
|
+
modelId === 'moonshot-v1-32k' ||
|
|
65
|
+
modelId === 'moonshot-v1-128k' ||
|
|
66
|
+
modelId === 'moonshot-v1-auto' ||
|
|
67
|
+
modelId === 'moonshot-v1-8k-vision-preview' ||
|
|
68
|
+
modelId === 'moonshot-v1-32k-vision-preview' ||
|
|
69
|
+
modelId === 'moonshot-v1-128k-vision-preview'
|
|
70
|
+
);
|
|
63
71
|
}
|
|
64
72
|
|
|
65
73
|
export function createMoonshotAI(
|