kimi-vercel-ai-sdk-provider 0.2.0
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/LICENSE +198 -0
- package/README.md +871 -0
- package/dist/index.d.mts +1317 -0
- package/dist/index.d.ts +1317 -0
- package/dist/index.js +2764 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2734 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +70 -0
- package/src/__tests__/caching.test.ts +97 -0
- package/src/__tests__/chat.test.ts +386 -0
- package/src/__tests__/code-integration.test.ts +562 -0
- package/src/__tests__/code-provider.test.ts +289 -0
- package/src/__tests__/code.test.ts +427 -0
- package/src/__tests__/core.test.ts +172 -0
- package/src/__tests__/files.test.ts +185 -0
- package/src/__tests__/integration.test.ts +457 -0
- package/src/__tests__/provider.test.ts +188 -0
- package/src/__tests__/tools.test.ts +519 -0
- package/src/chat/index.ts +42 -0
- package/src/chat/kimi-chat-language-model.ts +829 -0
- package/src/chat/kimi-chat-messages.ts +297 -0
- package/src/chat/kimi-chat-response.ts +84 -0
- package/src/chat/kimi-chat-settings.ts +216 -0
- package/src/code/index.ts +66 -0
- package/src/code/kimi-code-language-model.ts +669 -0
- package/src/code/kimi-code-messages.ts +303 -0
- package/src/code/kimi-code-provider.ts +239 -0
- package/src/code/kimi-code-settings.ts +193 -0
- package/src/code/kimi-code-types.ts +354 -0
- package/src/core/errors.ts +140 -0
- package/src/core/index.ts +36 -0
- package/src/core/types.ts +148 -0
- package/src/core/utils.ts +210 -0
- package/src/files/attachment-processor.ts +276 -0
- package/src/files/file-utils.ts +257 -0
- package/src/files/index.ts +24 -0
- package/src/files/kimi-file-client.ts +292 -0
- package/src/index.ts +122 -0
- package/src/kimi-provider.ts +263 -0
- package/src/tools/builtin-tools.ts +273 -0
- package/src/tools/index.ts +33 -0
- package/src/tools/prepare-tools.ts +306 -0
- package/src/version.ts +4 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,1317 @@
|
|
|
1
|
+
import { LanguageModelV3, LanguageModelV3Usage, LanguageModelV3CallOptions, LanguageModelV3GenerateResult, LanguageModelV3StreamResult, ProviderV3 } from '@ai-sdk/provider';
|
|
2
|
+
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
3
|
+
import { z } from 'zod/v4';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Base error class for Kimi provider errors.
|
|
7
|
+
*/
|
|
8
|
+
declare class KimiError extends Error {
|
|
9
|
+
readonly code: string;
|
|
10
|
+
readonly statusCode?: number;
|
|
11
|
+
constructor(message: string, code: string, statusCode?: number);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Error thrown when authentication fails.
|
|
15
|
+
*/
|
|
16
|
+
declare class KimiAuthenticationError extends KimiError {
|
|
17
|
+
constructor(message?: string);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Error thrown when rate limit is exceeded.
|
|
21
|
+
*/
|
|
22
|
+
declare class KimiRateLimitError extends KimiError {
|
|
23
|
+
readonly retryAfter?: number;
|
|
24
|
+
constructor(message?: string, retryAfter?: number);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Error thrown when the request is invalid.
|
|
28
|
+
*/
|
|
29
|
+
declare class KimiValidationError extends KimiError {
|
|
30
|
+
constructor(message: string, param?: string);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Error thrown when a model is not found.
|
|
34
|
+
*/
|
|
35
|
+
declare class KimiModelNotFoundError extends KimiError {
|
|
36
|
+
readonly modelId: string;
|
|
37
|
+
constructor(modelId: string);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Error thrown when content is filtered.
|
|
41
|
+
*/
|
|
42
|
+
declare class KimiContentFilterError extends KimiError {
|
|
43
|
+
constructor(message?: string);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Error thrown when context length is exceeded.
|
|
47
|
+
*/
|
|
48
|
+
declare class KimiContextLengthError extends KimiError {
|
|
49
|
+
constructor(message?: string);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Core types for the Kimi provider.
|
|
54
|
+
* @module
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Available Kimi chat model IDs.
|
|
59
|
+
*
|
|
60
|
+
* @remarks
|
|
61
|
+
* - `kimi-k2.5` - Latest flagship model with multimodal support
|
|
62
|
+
* - `kimi-k2.5-thinking` - K2.5 with always-on deep reasoning
|
|
63
|
+
* - `kimi-k2-turbo` - Fast, cost-effective model
|
|
64
|
+
* - `kimi-k2-thinking` - K2 with always-on deep reasoning
|
|
65
|
+
*/
|
|
66
|
+
type KimiChatModelId = 'kimi-k2.5' | 'kimi-k2.5-thinking' | 'kimi-k2-turbo' | 'kimi-k2-thinking' | (string & {});
|
|
67
|
+
/**
|
|
68
|
+
* Capabilities that can be detected from model ID patterns or explicitly set.
|
|
69
|
+
*/
|
|
70
|
+
interface KimiModelCapabilities {
|
|
71
|
+
/**
|
|
72
|
+
* Whether the model supports thinking/reasoning mode.
|
|
73
|
+
* Models with `-thinking` suffix have this enabled by default.
|
|
74
|
+
*/
|
|
75
|
+
thinking?: boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Whether the model always uses thinking mode (cannot be disabled).
|
|
78
|
+
* Thinking models like `kimi-k2.5-thinking` have this set to true.
|
|
79
|
+
*/
|
|
80
|
+
alwaysThinking?: boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Whether the model supports image inputs.
|
|
83
|
+
*/
|
|
84
|
+
imageInput?: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Whether the model supports video inputs.
|
|
87
|
+
* Currently only kimi-k2.5 models support video.
|
|
88
|
+
*/
|
|
89
|
+
videoInput?: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Maximum context window size in tokens.
|
|
92
|
+
*/
|
|
93
|
+
maxContextSize?: number;
|
|
94
|
+
/**
|
|
95
|
+
* Whether the model supports tool/function calling.
|
|
96
|
+
*/
|
|
97
|
+
toolCalling?: boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Whether the model supports JSON mode.
|
|
100
|
+
*/
|
|
101
|
+
jsonMode?: boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Whether the model supports structured outputs.
|
|
104
|
+
*/
|
|
105
|
+
structuredOutputs?: boolean;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Infer model capabilities from the model ID.
|
|
109
|
+
*
|
|
110
|
+
* @param modelId - The model identifier
|
|
111
|
+
* @returns Inferred capabilities based on model name patterns
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* const caps = inferModelCapabilities('kimi-k2.5-thinking');
|
|
116
|
+
* // { thinking: true, alwaysThinking: true, videoInput: true, ... }
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
declare function inferModelCapabilities(modelId: string): KimiModelCapabilities;
|
|
120
|
+
/**
|
|
121
|
+
* Configuration for the chat language model.
|
|
122
|
+
* @internal
|
|
123
|
+
*/
|
|
124
|
+
interface KimiChatConfig {
|
|
125
|
+
provider: string;
|
|
126
|
+
baseURL: string;
|
|
127
|
+
headers: () => Record<string, string | undefined>;
|
|
128
|
+
fetch?: typeof globalThis.fetch;
|
|
129
|
+
generateId?: () => string;
|
|
130
|
+
supportsStructuredOutputs?: boolean;
|
|
131
|
+
includeUsageInStream?: boolean;
|
|
132
|
+
supportedUrls?: LanguageModelV3['supportedUrls'];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Utility functions for the Kimi provider.
|
|
137
|
+
* @module
|
|
138
|
+
*/
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Extended usage information including web search and code interpreter tokens.
|
|
142
|
+
*/
|
|
143
|
+
interface KimiExtendedUsage extends LanguageModelV3Usage {
|
|
144
|
+
/**
|
|
145
|
+
* Tokens used by the built-in web search tool.
|
|
146
|
+
*/
|
|
147
|
+
webSearchTokens?: number;
|
|
148
|
+
/**
|
|
149
|
+
* Tokens used by the built-in code interpreter.
|
|
150
|
+
*/
|
|
151
|
+
codeInterpreterTokens?: number;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Built-in tools for Kimi API.
|
|
156
|
+
*
|
|
157
|
+
* Kimi provides server-side tools that can be invoked during chat completions:
|
|
158
|
+
* - `$web_search`: Search the web for information
|
|
159
|
+
* - `$code`: Execute code using Kimi's code interpreter
|
|
160
|
+
*
|
|
161
|
+
* @module
|
|
162
|
+
*/
|
|
163
|
+
/**
|
|
164
|
+
* Kimi's built-in web search tool identifier.
|
|
165
|
+
* This tool allows Kimi to search the web for up-to-date information.
|
|
166
|
+
*/
|
|
167
|
+
declare const KIMI_WEB_SEARCH_TOOL_NAME = "$web_search";
|
|
168
|
+
/**
|
|
169
|
+
* Kimi's built-in code interpreter tool identifier.
|
|
170
|
+
* This tool allows Kimi to execute code and return results.
|
|
171
|
+
*/
|
|
172
|
+
declare const KIMI_CODE_INTERPRETER_TOOL_NAME = "$code";
|
|
173
|
+
/**
|
|
174
|
+
* Configuration for Kimi's built-in web search tool.
|
|
175
|
+
*/
|
|
176
|
+
interface KimiWebSearchConfig {
|
|
177
|
+
/**
|
|
178
|
+
* Whether to include search results in the response.
|
|
179
|
+
* When true, the search results will be included in the tool output.
|
|
180
|
+
*/
|
|
181
|
+
search_result?: boolean;
|
|
182
|
+
/**
|
|
183
|
+
* Allow additional configuration options.
|
|
184
|
+
*/
|
|
185
|
+
[key: string]: unknown;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Configuration for Kimi's built-in code interpreter tool.
|
|
189
|
+
*/
|
|
190
|
+
interface KimiCodeInterpreterConfig {
|
|
191
|
+
/**
|
|
192
|
+
* Maximum execution time in seconds.
|
|
193
|
+
* Default varies by model and API tier.
|
|
194
|
+
*/
|
|
195
|
+
timeout?: number;
|
|
196
|
+
/**
|
|
197
|
+
* Whether to return execution output.
|
|
198
|
+
* When true, stdout/stderr will be included in results.
|
|
199
|
+
*/
|
|
200
|
+
include_output?: boolean;
|
|
201
|
+
/**
|
|
202
|
+
* Allow additional configuration options.
|
|
203
|
+
*/
|
|
204
|
+
[key: string]: unknown;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* A Kimi built-in tool definition.
|
|
208
|
+
* These are handled server-side by Kimi's API.
|
|
209
|
+
*/
|
|
210
|
+
interface KimiBuiltinTool {
|
|
211
|
+
type: 'builtin_function';
|
|
212
|
+
function: {
|
|
213
|
+
name: string;
|
|
214
|
+
config?: Record<string, unknown>;
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Configuration options for the web search tool.
|
|
219
|
+
*/
|
|
220
|
+
interface KimiWebSearchToolOptions {
|
|
221
|
+
/**
|
|
222
|
+
* Whether the web search tool is enabled.
|
|
223
|
+
*/
|
|
224
|
+
enabled: boolean;
|
|
225
|
+
/**
|
|
226
|
+
* Optional configuration for the web search tool.
|
|
227
|
+
*/
|
|
228
|
+
config?: KimiWebSearchConfig;
|
|
229
|
+
}
|
|
230
|
+
type KimiWebSearchToolConfig = KimiWebSearchToolOptions;
|
|
231
|
+
/**
|
|
232
|
+
* Create a Kimi built-in web search tool definition.
|
|
233
|
+
*
|
|
234
|
+
* @param config - Optional configuration for the web search tool
|
|
235
|
+
* @returns A built-in tool definition for $web_search
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ```ts
|
|
239
|
+
* // Basic usage
|
|
240
|
+
* const tool = createWebSearchTool();
|
|
241
|
+
*
|
|
242
|
+
* // With configuration
|
|
243
|
+
* const tool = createWebSearchTool({ search_result: true });
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
246
|
+
declare function createWebSearchTool(config?: KimiWebSearchConfig): KimiBuiltinTool;
|
|
247
|
+
declare function createKimiWebSearchTool(config?: KimiWebSearchConfig): KimiBuiltinTool;
|
|
248
|
+
/**
|
|
249
|
+
* Configuration options for the code interpreter tool.
|
|
250
|
+
*/
|
|
251
|
+
interface KimiCodeInterpreterToolOptions {
|
|
252
|
+
/**
|
|
253
|
+
* Whether the code interpreter tool is enabled.
|
|
254
|
+
*/
|
|
255
|
+
enabled: boolean;
|
|
256
|
+
/**
|
|
257
|
+
* Optional configuration for the code interpreter tool.
|
|
258
|
+
*/
|
|
259
|
+
config?: KimiCodeInterpreterConfig;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Create a Kimi built-in code interpreter tool definition.
|
|
263
|
+
*
|
|
264
|
+
* @param config - Optional configuration for the code interpreter tool
|
|
265
|
+
* @returns A built-in tool definition for $code
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```ts
|
|
269
|
+
* // Basic usage
|
|
270
|
+
* const tool = createCodeInterpreterTool();
|
|
271
|
+
*
|
|
272
|
+
* // With configuration
|
|
273
|
+
* const tool = createCodeInterpreterTool({ timeout: 30, include_output: true });
|
|
274
|
+
* ```
|
|
275
|
+
*/
|
|
276
|
+
declare function createCodeInterpreterTool(config?: KimiCodeInterpreterConfig): KimiBuiltinTool;
|
|
277
|
+
/**
|
|
278
|
+
* Create provider-level tool definitions for use with the AI SDK.
|
|
279
|
+
* These can be passed directly to the tools option.
|
|
280
|
+
*/
|
|
281
|
+
declare const kimiTools: {
|
|
282
|
+
/**
|
|
283
|
+
* Create a web search tool for use with Kimi models.
|
|
284
|
+
*
|
|
285
|
+
* @param config - Optional configuration
|
|
286
|
+
* @returns A provider tool definition
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
289
|
+
* ```ts
|
|
290
|
+
* import { kimi, kimiTools } from 'ai-sdk-provider-kimi';
|
|
291
|
+
*
|
|
292
|
+
* const result = await generateText({
|
|
293
|
+
* model: kimi('kimi-k2.5'),
|
|
294
|
+
* tools: {
|
|
295
|
+
* webSearch: kimiTools.webSearch(),
|
|
296
|
+
* },
|
|
297
|
+
* prompt: 'What are the latest AI news?',
|
|
298
|
+
* });
|
|
299
|
+
* ```
|
|
300
|
+
*/
|
|
301
|
+
webSearch: (config?: KimiWebSearchConfig) => {
|
|
302
|
+
type: "provider";
|
|
303
|
+
id: string;
|
|
304
|
+
args: KimiBuiltinTool;
|
|
305
|
+
};
|
|
306
|
+
/**
|
|
307
|
+
* Create a code interpreter tool for use with Kimi models.
|
|
308
|
+
*
|
|
309
|
+
* @param config - Optional configuration
|
|
310
|
+
* @returns A provider tool definition
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* ```ts
|
|
314
|
+
* import { kimi, kimiTools } from 'ai-sdk-provider-kimi';
|
|
315
|
+
*
|
|
316
|
+
* const result = await generateText({
|
|
317
|
+
* model: kimi('kimi-k2.5'),
|
|
318
|
+
* tools: {
|
|
319
|
+
* codeInterpreter: kimiTools.codeInterpreter(),
|
|
320
|
+
* },
|
|
321
|
+
* prompt: 'Calculate the factorial of 10',
|
|
322
|
+
* });
|
|
323
|
+
* ```
|
|
324
|
+
*/
|
|
325
|
+
codeInterpreter: (config?: KimiCodeInterpreterConfig) => {
|
|
326
|
+
type: "provider";
|
|
327
|
+
id: string;
|
|
328
|
+
args: KimiBuiltinTool;
|
|
329
|
+
};
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Chat model settings and provider options schema.
|
|
334
|
+
* @module
|
|
335
|
+
*/
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Configuration for context caching.
|
|
339
|
+
* Enables cost reduction for long, repeated prompts.
|
|
340
|
+
*/
|
|
341
|
+
interface KimiCachingConfig {
|
|
342
|
+
/**
|
|
343
|
+
* Enable context caching for this request.
|
|
344
|
+
*/
|
|
345
|
+
enabled: boolean;
|
|
346
|
+
/**
|
|
347
|
+
* Optional cache key for identifying cached context.
|
|
348
|
+
* Use the same key across requests to hit the same cache.
|
|
349
|
+
*/
|
|
350
|
+
cacheKey?: string;
|
|
351
|
+
/**
|
|
352
|
+
* Time-to-live for the cache in seconds.
|
|
353
|
+
* Defaults to API default (typically 3600 seconds / 1 hour).
|
|
354
|
+
*/
|
|
355
|
+
ttlSeconds?: number;
|
|
356
|
+
/**
|
|
357
|
+
* Reset the cache even if a matching cache exists.
|
|
358
|
+
*/
|
|
359
|
+
resetCache?: boolean;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Settings for creating a Kimi chat model instance.
|
|
363
|
+
*/
|
|
364
|
+
interface KimiChatSettings {
|
|
365
|
+
/**
|
|
366
|
+
* Enable JSON schema structured outputs when a schema is provided.
|
|
367
|
+
*/
|
|
368
|
+
supportsStructuredOutputs?: boolean;
|
|
369
|
+
/**
|
|
370
|
+
* Request usage metrics during streaming (if supported by the API).
|
|
371
|
+
*/
|
|
372
|
+
includeUsageInStream?: boolean;
|
|
373
|
+
/**
|
|
374
|
+
* Override supported URL patterns for file parts.
|
|
375
|
+
*/
|
|
376
|
+
supportedUrls?: LanguageModelV3['supportedUrls'];
|
|
377
|
+
/**
|
|
378
|
+
* Enable the built-in web search tool.
|
|
379
|
+
* When true, Kimi can search the web to answer questions.
|
|
380
|
+
* You can also pass a configuration object for more control.
|
|
381
|
+
*/
|
|
382
|
+
webSearch?: boolean | KimiWebSearchToolOptions;
|
|
383
|
+
/**
|
|
384
|
+
* Enable the built-in code interpreter tool.
|
|
385
|
+
* When true, Kimi can execute code to solve problems.
|
|
386
|
+
* You can also pass a configuration object for more control.
|
|
387
|
+
*/
|
|
388
|
+
codeInterpreter?: boolean | KimiCodeInterpreterToolOptions;
|
|
389
|
+
/**
|
|
390
|
+
* Override inferred model capabilities.
|
|
391
|
+
*/
|
|
392
|
+
capabilities?: Partial<KimiModelCapabilities>;
|
|
393
|
+
/**
|
|
394
|
+
* Enable tool choice polyfill for unsupported tool choice modes.
|
|
395
|
+
* When true (default), uses system message injection to simulate
|
|
396
|
+
* `required` and `tool` choices that Kimi doesn't natively support.
|
|
397
|
+
*
|
|
398
|
+
* @default true
|
|
399
|
+
*/
|
|
400
|
+
toolChoicePolyfill?: boolean;
|
|
401
|
+
/**
|
|
402
|
+
* Enable automatic file handling for experimental_attachments.
|
|
403
|
+
* When true, PDFs and documents will be automatically uploaded
|
|
404
|
+
* to Kimi's file API and their content injected into the context.
|
|
405
|
+
*
|
|
406
|
+
* @default false
|
|
407
|
+
*/
|
|
408
|
+
autoFileUpload?: boolean;
|
|
409
|
+
/**
|
|
410
|
+
* Context caching configuration.
|
|
411
|
+
* Reduces costs by up to 90% for repeated long prompts.
|
|
412
|
+
*/
|
|
413
|
+
caching?: boolean | KimiCachingConfig;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Zod schema for caching configuration.
|
|
417
|
+
*/
|
|
418
|
+
declare const kimiCachingConfigSchema: z.ZodObject<{
|
|
419
|
+
enabled: z.ZodBoolean;
|
|
420
|
+
cacheKey: z.ZodOptional<z.ZodString>;
|
|
421
|
+
ttlSeconds: z.ZodOptional<z.ZodNumber>;
|
|
422
|
+
resetCache: z.ZodOptional<z.ZodBoolean>;
|
|
423
|
+
}, z.core.$strip>;
|
|
424
|
+
/**
|
|
425
|
+
* Zod schema for validating provider options passed to individual calls.
|
|
426
|
+
*/
|
|
427
|
+
declare const kimiProviderOptionsSchema: z.ZodObject<{
|
|
428
|
+
user: z.ZodOptional<z.ZodString>;
|
|
429
|
+
strictJsonSchema: z.ZodOptional<z.ZodBoolean>;
|
|
430
|
+
requestId: z.ZodOptional<z.ZodString>;
|
|
431
|
+
extraHeaders: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
432
|
+
parallelToolCalls: z.ZodOptional<z.ZodBoolean>;
|
|
433
|
+
webSearch: z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodObject<{
|
|
434
|
+
enabled: z.ZodBoolean;
|
|
435
|
+
config: z.ZodOptional<z.ZodObject<{
|
|
436
|
+
search_result: z.ZodOptional<z.ZodBoolean>;
|
|
437
|
+
}, z.core.$strip>>;
|
|
438
|
+
}, z.core.$strip>]>>;
|
|
439
|
+
codeInterpreter: z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodObject<{
|
|
440
|
+
enabled: z.ZodBoolean;
|
|
441
|
+
config: z.ZodOptional<z.ZodObject<{
|
|
442
|
+
timeout: z.ZodOptional<z.ZodNumber>;
|
|
443
|
+
include_output: z.ZodOptional<z.ZodBoolean>;
|
|
444
|
+
}, z.core.$strip>>;
|
|
445
|
+
}, z.core.$strip>]>>;
|
|
446
|
+
caching: z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodObject<{
|
|
447
|
+
enabled: z.ZodBoolean;
|
|
448
|
+
cacheKey: z.ZodOptional<z.ZodString>;
|
|
449
|
+
ttlSeconds: z.ZodOptional<z.ZodNumber>;
|
|
450
|
+
resetCache: z.ZodOptional<z.ZodBoolean>;
|
|
451
|
+
}, z.core.$strip>]>>;
|
|
452
|
+
toolChoicePolyfill: z.ZodOptional<z.ZodBoolean>;
|
|
453
|
+
}, z.core.$strip>;
|
|
454
|
+
/**
|
|
455
|
+
* Type for provider options passed to individual calls.
|
|
456
|
+
*/
|
|
457
|
+
type KimiProviderOptions = z.infer<typeof kimiProviderOptionsSchema>;
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Kimi chat language model implementation.
|
|
461
|
+
* @module
|
|
462
|
+
*/
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Kimi chat language model implementing LanguageModelV3.
|
|
466
|
+
*/
|
|
467
|
+
declare class KimiChatLanguageModel implements LanguageModelV3 {
|
|
468
|
+
readonly specificationVersion = "v3";
|
|
469
|
+
readonly modelId: KimiChatModelId;
|
|
470
|
+
private readonly config;
|
|
471
|
+
private readonly settings;
|
|
472
|
+
private readonly generateIdFn;
|
|
473
|
+
private readonly supportsStructuredOutputs;
|
|
474
|
+
constructor(modelId: KimiChatModelId, settings: KimiChatSettings, config: KimiChatConfig);
|
|
475
|
+
get provider(): string;
|
|
476
|
+
private get providerOptionsName();
|
|
477
|
+
/**
|
|
478
|
+
* Get the inferred or configured capabilities for this model.
|
|
479
|
+
*/
|
|
480
|
+
get capabilities(): {
|
|
481
|
+
thinking?: boolean;
|
|
482
|
+
alwaysThinking?: boolean;
|
|
483
|
+
imageInput?: boolean;
|
|
484
|
+
videoInput?: boolean;
|
|
485
|
+
maxContextSize?: number;
|
|
486
|
+
toolCalling?: boolean;
|
|
487
|
+
jsonMode?: boolean;
|
|
488
|
+
structuredOutputs?: boolean;
|
|
489
|
+
};
|
|
490
|
+
get supportedUrls(): Record<string, RegExp[]> | PromiseLike<Record<string, RegExp[]>>;
|
|
491
|
+
private getArgs;
|
|
492
|
+
doGenerate(options: LanguageModelV3CallOptions): Promise<LanguageModelV3GenerateResult>;
|
|
493
|
+
doStream(options: LanguageModelV3CallOptions): Promise<LanguageModelV3StreamResult>;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Kimi File API client for uploading and managing files.
|
|
498
|
+
* @module
|
|
499
|
+
*/
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* A file object returned by the Kimi File API.
|
|
503
|
+
*/
|
|
504
|
+
interface KimiFile {
|
|
505
|
+
/** Unique file identifier */
|
|
506
|
+
id: string;
|
|
507
|
+
/** File size in bytes */
|
|
508
|
+
bytes: number;
|
|
509
|
+
/** Unix timestamp of creation */
|
|
510
|
+
created_at: number;
|
|
511
|
+
/** Original filename */
|
|
512
|
+
filename: string;
|
|
513
|
+
/** Object type (always 'file') */
|
|
514
|
+
object: 'file';
|
|
515
|
+
/** File purpose (file-extract, image, video) */
|
|
516
|
+
purpose: 'file-extract' | 'image' | 'video';
|
|
517
|
+
/** Processing status */
|
|
518
|
+
status: 'ok' | 'error' | 'processing';
|
|
519
|
+
/** Status details if error */
|
|
520
|
+
status_details?: string;
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Options for uploading a file.
|
|
524
|
+
*/
|
|
525
|
+
interface FileUploadOptions {
|
|
526
|
+
/** The file data as a Buffer, Uint8Array, or string (base64) */
|
|
527
|
+
data: Uint8Array | string;
|
|
528
|
+
/** The filename */
|
|
529
|
+
filename: string;
|
|
530
|
+
/** MIME type of the file */
|
|
531
|
+
mediaType?: string;
|
|
532
|
+
/** Purpose of the file (defaults based on mediaType) */
|
|
533
|
+
purpose?: 'file-extract' | 'image' | 'video';
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Result of a file upload operation.
|
|
537
|
+
*/
|
|
538
|
+
interface FileUploadResult {
|
|
539
|
+
/** The uploaded file object */
|
|
540
|
+
file: KimiFile;
|
|
541
|
+
/** The extracted content (for file-extract purpose) */
|
|
542
|
+
content?: string;
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Configuration for the file client.
|
|
546
|
+
*/
|
|
547
|
+
interface KimiFileClientConfig {
|
|
548
|
+
/** Base URL for the API */
|
|
549
|
+
baseURL: string;
|
|
550
|
+
/** Function to get authorization headers */
|
|
551
|
+
headers: () => Record<string, string | undefined>;
|
|
552
|
+
/** Custom fetch implementation */
|
|
553
|
+
fetch?: FetchFunction;
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Client for interacting with Kimi's File API.
|
|
557
|
+
*
|
|
558
|
+
* Supports uploading files for content extraction, image understanding,
|
|
559
|
+
* and video understanding.
|
|
560
|
+
*
|
|
561
|
+
* @example
|
|
562
|
+
* ```ts
|
|
563
|
+
* const client = new KimiFileClient({
|
|
564
|
+
* baseURL: 'https://api.moonshot.ai/v1',
|
|
565
|
+
* headers: () => ({
|
|
566
|
+
* Authorization: `Bearer ${apiKey}`,
|
|
567
|
+
* }),
|
|
568
|
+
* });
|
|
569
|
+
*
|
|
570
|
+
* // Upload a PDF and extract content
|
|
571
|
+
* const result = await client.uploadAndExtract({
|
|
572
|
+
* data: pdfBuffer,
|
|
573
|
+
* filename: 'document.pdf',
|
|
574
|
+
* mediaType: 'application/pdf',
|
|
575
|
+
* });
|
|
576
|
+
*
|
|
577
|
+
* console.log(result.content); // Extracted text content
|
|
578
|
+
* ```
|
|
579
|
+
*/
|
|
580
|
+
declare class KimiFileClient {
|
|
581
|
+
private readonly config;
|
|
582
|
+
constructor(config: KimiFileClientConfig);
|
|
583
|
+
/**
|
|
584
|
+
* Upload a file to the Kimi API.
|
|
585
|
+
*/
|
|
586
|
+
upload(options: FileUploadOptions): Promise<KimiFile>;
|
|
587
|
+
/**
|
|
588
|
+
* Get the content of an uploaded file (for file-extract purpose).
|
|
589
|
+
*/
|
|
590
|
+
getContent(fileId: string): Promise<string>;
|
|
591
|
+
/**
|
|
592
|
+
* Upload a file and extract its content in one operation.
|
|
593
|
+
* Only works for files with purpose="file-extract".
|
|
594
|
+
*/
|
|
595
|
+
uploadAndExtract(options: FileUploadOptions): Promise<FileUploadResult>;
|
|
596
|
+
/**
|
|
597
|
+
* Get file information.
|
|
598
|
+
*/
|
|
599
|
+
getFile(fileId: string): Promise<KimiFile>;
|
|
600
|
+
/**
|
|
601
|
+
* List all uploaded files.
|
|
602
|
+
*/
|
|
603
|
+
listFiles(): Promise<KimiFile[]>;
|
|
604
|
+
/**
|
|
605
|
+
* Delete a file.
|
|
606
|
+
*/
|
|
607
|
+
deleteFile(fileId: string): Promise<void>;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* Attachment processor for experimental_attachments support.
|
|
612
|
+
* Automatically uploads files to Kimi and injects content into prompts.
|
|
613
|
+
* @module
|
|
614
|
+
*/
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
* An attachment from experimental_attachments.
|
|
618
|
+
*/
|
|
619
|
+
interface Attachment {
|
|
620
|
+
/** URL of the attachment */
|
|
621
|
+
url?: string;
|
|
622
|
+
/** Name of the attachment */
|
|
623
|
+
name?: string;
|
|
624
|
+
/** MIME type */
|
|
625
|
+
contentType?: string;
|
|
626
|
+
/** Raw content data */
|
|
627
|
+
content?: Uint8Array | string;
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* Processed attachment result.
|
|
631
|
+
*/
|
|
632
|
+
interface ProcessedAttachment {
|
|
633
|
+
/** Original attachment */
|
|
634
|
+
original: Attachment;
|
|
635
|
+
/** Processing type */
|
|
636
|
+
type: 'text-inject' | 'image-url' | 'video-url' | 'skip';
|
|
637
|
+
/** Extracted text content (for documents) */
|
|
638
|
+
textContent?: string;
|
|
639
|
+
/** URL to use in message (for images/videos) */
|
|
640
|
+
mediaUrl?: string;
|
|
641
|
+
/** Kimi file ID (if uploaded) */
|
|
642
|
+
fileId?: string;
|
|
643
|
+
/** Error if processing failed */
|
|
644
|
+
error?: string;
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* Options for processing attachments.
|
|
648
|
+
*/
|
|
649
|
+
interface ProcessAttachmentsOptions {
|
|
650
|
+
/** Attachments to process */
|
|
651
|
+
attachments: Attachment[];
|
|
652
|
+
/** File client configuration */
|
|
653
|
+
clientConfig: KimiFileClientConfig;
|
|
654
|
+
/** Whether to auto-upload documents for extraction */
|
|
655
|
+
autoUploadDocuments?: boolean;
|
|
656
|
+
/** Whether to upload images to Kimi's file API */
|
|
657
|
+
uploadImages?: boolean;
|
|
658
|
+
/** Whether to delete files after extraction (cleanup) */
|
|
659
|
+
cleanupAfterExtract?: boolean;
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* Process experimental_attachments for Kimi.
|
|
663
|
+
*
|
|
664
|
+
* This function handles different attachment types:
|
|
665
|
+
* - Documents (PDF, DOC, etc.): Uploads to Kimi, extracts content, returns text to inject
|
|
666
|
+
* - Images: Returns URL for vision input
|
|
667
|
+
* - Videos: Returns URL for video input
|
|
668
|
+
*
|
|
669
|
+
* @example
|
|
670
|
+
* ```ts
|
|
671
|
+
* const processed = await processAttachments({
|
|
672
|
+
* attachments: message.experimental_attachments ?? [],
|
|
673
|
+
* clientConfig: {
|
|
674
|
+
* baseURL: 'https://api.moonshot.ai/v1',
|
|
675
|
+
* headers: () => ({ Authorization: `Bearer ${apiKey}` }),
|
|
676
|
+
* },
|
|
677
|
+
* });
|
|
678
|
+
*
|
|
679
|
+
* // Inject document content into system messages
|
|
680
|
+
* const documentContent = processed
|
|
681
|
+
* .filter(p => p.type === 'text-inject' && p.textContent)
|
|
682
|
+
* .map(p => p.textContent)
|
|
683
|
+
* .join('\n');
|
|
684
|
+
* ```
|
|
685
|
+
*/
|
|
686
|
+
declare function processAttachments(options: ProcessAttachmentsOptions): Promise<ProcessedAttachment[]>;
|
|
687
|
+
|
|
688
|
+
/**
|
|
689
|
+
* File utility functions for Kimi API.
|
|
690
|
+
* @module
|
|
691
|
+
*/
|
|
692
|
+
/**
|
|
693
|
+
* File extensions supported by Kimi's file upload API.
|
|
694
|
+
*/
|
|
695
|
+
declare const SUPPORTED_FILE_EXTENSIONS: readonly [".pdf", ".txt", ".csv", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".md", ".epub", ".mobi", ".html", ".json", ".log", ".dot", ".ini", ".conf", ".yaml", ".yml", ".jpeg", ".jpg", ".png", ".bmp", ".gif", ".svg", ".svgz", ".webp", ".ico", ".xbm", ".dib", ".pjp", ".tif", ".tiff", ".pjpeg", ".avif", ".apng", ".jfif", ".go", ".h", ".c", ".cpp", ".cxx", ".cc", ".cs", ".java", ".js", ".css", ".jsp", ".php", ".py", ".py3", ".asp", ".ts", ".tsx"];
|
|
696
|
+
/**
|
|
697
|
+
* MIME types supported by Kimi's file upload API.
|
|
698
|
+
*/
|
|
699
|
+
declare const SUPPORTED_MIME_TYPES: {
|
|
700
|
+
readonly 'application/pdf': "file-extract";
|
|
701
|
+
readonly 'text/plain': "file-extract";
|
|
702
|
+
readonly 'text/csv': "file-extract";
|
|
703
|
+
readonly 'application/msword': "file-extract";
|
|
704
|
+
readonly 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': "file-extract";
|
|
705
|
+
readonly 'application/vnd.ms-excel': "file-extract";
|
|
706
|
+
readonly 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': "file-extract";
|
|
707
|
+
readonly 'application/vnd.ms-powerpoint': "file-extract";
|
|
708
|
+
readonly 'application/vnd.openxmlformats-officedocument.presentationml.presentation': "file-extract";
|
|
709
|
+
readonly 'text/markdown': "file-extract";
|
|
710
|
+
readonly 'text/html': "file-extract";
|
|
711
|
+
readonly 'application/json': "file-extract";
|
|
712
|
+
readonly 'application/epub+zip': "file-extract";
|
|
713
|
+
readonly 'text/yaml': "file-extract";
|
|
714
|
+
readonly 'application/x-yaml': "file-extract";
|
|
715
|
+
readonly 'text/javascript': "file-extract";
|
|
716
|
+
readonly 'text/typescript': "file-extract";
|
|
717
|
+
readonly 'text/x-python': "file-extract";
|
|
718
|
+
readonly 'text/x-java': "file-extract";
|
|
719
|
+
readonly 'text/x-c': "file-extract";
|
|
720
|
+
readonly 'text/x-c++': "file-extract";
|
|
721
|
+
readonly 'text/css': "file-extract";
|
|
722
|
+
readonly 'image/jpeg': "image";
|
|
723
|
+
readonly 'image/png': "image";
|
|
724
|
+
readonly 'image/gif': "image";
|
|
725
|
+
readonly 'image/webp': "image";
|
|
726
|
+
readonly 'image/svg+xml': "image";
|
|
727
|
+
readonly 'image/bmp': "image";
|
|
728
|
+
readonly 'image/tiff': "image";
|
|
729
|
+
readonly 'image/avif': "image";
|
|
730
|
+
readonly 'image/apng': "image";
|
|
731
|
+
readonly 'image/x-icon': "image";
|
|
732
|
+
readonly 'video/mp4': "video";
|
|
733
|
+
readonly 'video/webm': "video";
|
|
734
|
+
readonly 'video/ogg': "video";
|
|
735
|
+
readonly 'video/quicktime': "video";
|
|
736
|
+
};
|
|
737
|
+
type FilePurpose = 'file-extract' | 'image' | 'video';
|
|
738
|
+
/**
|
|
739
|
+
* Check if a media type is for image files.
|
|
740
|
+
*/
|
|
741
|
+
declare function isImageMediaType(mediaType: string): boolean;
|
|
742
|
+
/**
|
|
743
|
+
* Check if a media type is for video files.
|
|
744
|
+
*/
|
|
745
|
+
declare function isVideoMediaType(mediaType: string): boolean;
|
|
746
|
+
/**
|
|
747
|
+
* Check if a media type should use file-extract purpose.
|
|
748
|
+
*/
|
|
749
|
+
declare function isFileExtractMediaType(mediaType: string): boolean;
|
|
750
|
+
/**
|
|
751
|
+
* Check if a media type is for document files (PDFs, Word, etc.).
|
|
752
|
+
*/
|
|
753
|
+
declare function isDocumentMediaType(mediaType: string): boolean;
|
|
754
|
+
/**
|
|
755
|
+
* Get the file purpose based on media type.
|
|
756
|
+
*/
|
|
757
|
+
declare function getPurposeFromMediaType(mediaType: string): FilePurpose;
|
|
758
|
+
/**
|
|
759
|
+
* Get MIME type from file extension.
|
|
760
|
+
*/
|
|
761
|
+
declare function getMediaTypeFromExtension(extension: string): string;
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* Kimi provider factory.
|
|
765
|
+
* @module
|
|
766
|
+
*/
|
|
767
|
+
|
|
768
|
+
/**
|
|
769
|
+
* Settings for creating a Kimi provider instance.
|
|
770
|
+
*/
|
|
771
|
+
interface KimiProviderSettings {
|
|
772
|
+
/**
|
|
773
|
+
* Moonshot AI API key. Defaults to the MOONSHOT_API_KEY environment variable.
|
|
774
|
+
*/
|
|
775
|
+
apiKey?: string;
|
|
776
|
+
/**
|
|
777
|
+
* Base URL override. Defaults to the global or China endpoint.
|
|
778
|
+
*/
|
|
779
|
+
baseURL?: string;
|
|
780
|
+
/**
|
|
781
|
+
* Select the regional endpoint when baseURL is not provided.
|
|
782
|
+
* - `global`: Use the global API endpoint (api.moonshot.ai)
|
|
783
|
+
* - `cn`: Use the China API endpoint (api.moonshot.cn)
|
|
784
|
+
*
|
|
785
|
+
* @default 'global'
|
|
786
|
+
*/
|
|
787
|
+
endpoint?: 'global' | 'cn';
|
|
788
|
+
/**
|
|
789
|
+
* Default headers for all requests.
|
|
790
|
+
*/
|
|
791
|
+
headers?: Record<string, string | undefined>;
|
|
792
|
+
/**
|
|
793
|
+
* Custom fetch implementation.
|
|
794
|
+
*/
|
|
795
|
+
fetch?: FetchFunction;
|
|
796
|
+
/**
|
|
797
|
+
* ID generator for tool call fallback IDs.
|
|
798
|
+
*/
|
|
799
|
+
generateId?: () => string;
|
|
800
|
+
/**
|
|
801
|
+
* Enable JSON schema structured outputs by default.
|
|
802
|
+
*/
|
|
803
|
+
supportsStructuredOutputs?: boolean;
|
|
804
|
+
/**
|
|
805
|
+
* Include usage details in streaming responses if supported.
|
|
806
|
+
*/
|
|
807
|
+
includeUsageInStream?: boolean;
|
|
808
|
+
/**
|
|
809
|
+
* Override supported URL patterns for file parts.
|
|
810
|
+
*/
|
|
811
|
+
supportedUrls?: LanguageModelV3['supportedUrls'];
|
|
812
|
+
}
|
|
813
|
+
/**
|
|
814
|
+
* The Kimi provider interface.
|
|
815
|
+
*/
|
|
816
|
+
interface KimiProvider extends Omit<ProviderV3, 'specificationVersion'> {
|
|
817
|
+
specificationVersion: 'v3';
|
|
818
|
+
/**
|
|
819
|
+
* Creates a chat language model.
|
|
820
|
+
* @param modelId - The model identifier
|
|
821
|
+
* @param settings - Optional model settings
|
|
822
|
+
*/
|
|
823
|
+
(modelId: KimiChatModelId, settings?: KimiChatSettings): LanguageModelV3;
|
|
824
|
+
/**
|
|
825
|
+
* Creates a chat language model.
|
|
826
|
+
* @param modelId - The model identifier
|
|
827
|
+
* @param settings - Optional model settings
|
|
828
|
+
*/
|
|
829
|
+
languageModel(modelId: KimiChatModelId, settings?: KimiChatSettings): LanguageModelV3;
|
|
830
|
+
/**
|
|
831
|
+
* Creates a chat language model (alias for languageModel).
|
|
832
|
+
* @param modelId - The model identifier
|
|
833
|
+
* @param settings - Optional model settings
|
|
834
|
+
*/
|
|
835
|
+
chat(modelId: KimiChatModelId, settings?: KimiChatSettings): LanguageModelV3;
|
|
836
|
+
/**
|
|
837
|
+
* Built-in tools that can be used with Kimi models.
|
|
838
|
+
*/
|
|
839
|
+
tools: typeof kimiTools;
|
|
840
|
+
/**
|
|
841
|
+
* File client for uploading and extracting content from files.
|
|
842
|
+
* Pre-configured with the provider's API key and base URL.
|
|
843
|
+
*
|
|
844
|
+
* @example
|
|
845
|
+
* ```ts
|
|
846
|
+
* const kimi = createKimi();
|
|
847
|
+
* const result = await kimi.files.uploadAndExtract({
|
|
848
|
+
* data: pdfBuffer,
|
|
849
|
+
* filename: 'document.pdf',
|
|
850
|
+
* });
|
|
851
|
+
* console.log(result.content);
|
|
852
|
+
* ```
|
|
853
|
+
*/
|
|
854
|
+
files: KimiFileClient;
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Create a Kimi provider instance.
|
|
858
|
+
*
|
|
859
|
+
* @param options - Provider settings
|
|
860
|
+
* @returns A configured Kimi provider
|
|
861
|
+
*
|
|
862
|
+
* @example
|
|
863
|
+
* ```ts
|
|
864
|
+
* import { createKimi } from 'ai-sdk-provider-kimi';
|
|
865
|
+
*
|
|
866
|
+
* const kimi = createKimi({
|
|
867
|
+
* apiKey: process.env.MOONSHOT_API_KEY,
|
|
868
|
+
* });
|
|
869
|
+
*
|
|
870
|
+
* const result = await generateText({
|
|
871
|
+
* model: kimi('kimi-k2.5'),
|
|
872
|
+
* prompt: 'Hello!',
|
|
873
|
+
* });
|
|
874
|
+
* ```
|
|
875
|
+
*
|
|
876
|
+
* @example
|
|
877
|
+
* ```ts
|
|
878
|
+
* // With web search enabled
|
|
879
|
+
* const result = await generateText({
|
|
880
|
+
* model: kimi('kimi-k2.5', { webSearch: true }),
|
|
881
|
+
* prompt: 'What are the latest AI news?',
|
|
882
|
+
* });
|
|
883
|
+
* ```
|
|
884
|
+
*
|
|
885
|
+
* @example
|
|
886
|
+
* ```ts
|
|
887
|
+
* // With code interpreter
|
|
888
|
+
* const result = await generateText({
|
|
889
|
+
* model: kimi('kimi-k2.5', { codeInterpreter: true }),
|
|
890
|
+
* prompt: 'Calculate the factorial of 20',
|
|
891
|
+
* });
|
|
892
|
+
* ```
|
|
893
|
+
*/
|
|
894
|
+
declare function createKimi(options?: KimiProviderSettings): KimiProvider;
|
|
895
|
+
/**
|
|
896
|
+
* Default Kimi provider instance.
|
|
897
|
+
*
|
|
898
|
+
* Uses the MOONSHOT_API_KEY environment variable for authentication.
|
|
899
|
+
*
|
|
900
|
+
* @example
|
|
901
|
+
* ```ts
|
|
902
|
+
* import { kimi } from 'ai-sdk-provider-kimi';
|
|
903
|
+
*
|
|
904
|
+
* const result = await generateText({
|
|
905
|
+
* model: kimi('kimi-k2.5'),
|
|
906
|
+
* prompt: 'Hello!',
|
|
907
|
+
* });
|
|
908
|
+
* ```
|
|
909
|
+
*/
|
|
910
|
+
declare const kimi: KimiProvider;
|
|
911
|
+
|
|
912
|
+
/**
|
|
913
|
+
* Types for the Kimi Code provider.
|
|
914
|
+
*
|
|
915
|
+
* Kimi Code is a premium coding service within the Kimi ecosystem that provides:
|
|
916
|
+
* - High-speed output (up to 100 tokens/s)
|
|
917
|
+
* - Extended thinking/reasoning support
|
|
918
|
+
* - Full compatibility with Claude Code and Roo Code
|
|
919
|
+
* - Anthropic-compatible API format
|
|
920
|
+
*
|
|
921
|
+
* @see https://www.kimi.com/code/docs/en/
|
|
922
|
+
* @module
|
|
923
|
+
*/
|
|
924
|
+
|
|
925
|
+
/**
|
|
926
|
+
* Default Kimi Code API endpoint (Anthropic-compatible).
|
|
927
|
+
* Used with Claude Code - set as ANTHROPIC_BASE_URL
|
|
928
|
+
*/
|
|
929
|
+
declare const KIMI_CODE_BASE_URL = "https://api.kimi.com/coding/v1";
|
|
930
|
+
/**
|
|
931
|
+
* Default Kimi Code model ID.
|
|
932
|
+
* Primary coding model optimized for development tasks.
|
|
933
|
+
*/
|
|
934
|
+
declare const KIMI_CODE_DEFAULT_MODEL = "kimi-for-coding";
|
|
935
|
+
/**
|
|
936
|
+
* Alternative model with enhanced thinking/reasoning.
|
|
937
|
+
* Can be toggled with Tab key in Claude Code.
|
|
938
|
+
*/
|
|
939
|
+
declare const KIMI_CODE_THINKING_MODEL = "kimi-k2-thinking";
|
|
940
|
+
/**
|
|
941
|
+
* Available Kimi Code model IDs.
|
|
942
|
+
*
|
|
943
|
+
* @remarks
|
|
944
|
+
* - `kimi-for-coding` - Primary coding model optimized for development tasks
|
|
945
|
+
* - `kimi-k2-thinking` - Model with extended thinking for complex reasoning (toggle with Tab in Claude Code)
|
|
946
|
+
*
|
|
947
|
+
* @example
|
|
948
|
+
* ```ts
|
|
949
|
+
* // Default model
|
|
950
|
+
* const model = kimiCode('kimi-for-coding');
|
|
951
|
+
*
|
|
952
|
+
* // Thinking model
|
|
953
|
+
* const thinkingModel = kimiCode('kimi-k2-thinking');
|
|
954
|
+
* ```
|
|
955
|
+
*/
|
|
956
|
+
type KimiCodeModelId = 'kimi-for-coding' | 'kimi-k2-thinking' | (string & {});
|
|
957
|
+
/**
|
|
958
|
+
* Capabilities specific to Kimi Code models.
|
|
959
|
+
* Based on Roo Code configuration documentation.
|
|
960
|
+
*/
|
|
961
|
+
interface KimiCodeCapabilities {
|
|
962
|
+
/**
|
|
963
|
+
* Whether the model supports extended thinking/reasoning.
|
|
964
|
+
* When enabled, use `thinking.type: 'enabled'` with `budget_tokens`.
|
|
965
|
+
*/
|
|
966
|
+
extendedThinking?: boolean;
|
|
967
|
+
/**
|
|
968
|
+
* Maximum output tokens.
|
|
969
|
+
* Default: 32768 (per Roo Code docs)
|
|
970
|
+
*/
|
|
971
|
+
maxOutputTokens?: number;
|
|
972
|
+
/**
|
|
973
|
+
* Maximum context window size.
|
|
974
|
+
* Default: 262144 (per Roo Code docs)
|
|
975
|
+
*/
|
|
976
|
+
maxContextSize?: number;
|
|
977
|
+
/**
|
|
978
|
+
* Whether the model supports streaming.
|
|
979
|
+
* Always true for Kimi Code.
|
|
980
|
+
*/
|
|
981
|
+
streaming?: boolean;
|
|
982
|
+
/**
|
|
983
|
+
* Whether the model supports tool/function calling.
|
|
984
|
+
* Always true for Kimi Code.
|
|
985
|
+
*/
|
|
986
|
+
toolCalling?: boolean;
|
|
987
|
+
/**
|
|
988
|
+
* Whether the model supports image inputs.
|
|
989
|
+
*/
|
|
990
|
+
imageInput?: boolean;
|
|
991
|
+
}
|
|
992
|
+
/**
|
|
993
|
+
* Infer model capabilities from the model ID.
|
|
994
|
+
*
|
|
995
|
+
* @param modelId - The model identifier
|
|
996
|
+
* @returns Inferred capabilities based on model name patterns
|
|
997
|
+
*
|
|
998
|
+
* @example
|
|
999
|
+
* ```ts
|
|
1000
|
+
* const caps = inferKimiCodeCapabilities('kimi-k2-thinking');
|
|
1001
|
+
* // caps.extendedThinking === true
|
|
1002
|
+
* ```
|
|
1003
|
+
*/
|
|
1004
|
+
declare function inferKimiCodeCapabilities(modelId: string): KimiCodeCapabilities;
|
|
1005
|
+
/**
|
|
1006
|
+
* Configuration for the Kimi Code language model.
|
|
1007
|
+
* @internal
|
|
1008
|
+
*/
|
|
1009
|
+
interface KimiCodeConfig {
|
|
1010
|
+
/**
|
|
1011
|
+
* Provider identifier.
|
|
1012
|
+
*/
|
|
1013
|
+
provider: string;
|
|
1014
|
+
/**
|
|
1015
|
+
* Base URL for the API.
|
|
1016
|
+
*/
|
|
1017
|
+
baseURL: string;
|
|
1018
|
+
/**
|
|
1019
|
+
* Function to get headers for requests.
|
|
1020
|
+
*/
|
|
1021
|
+
headers: () => Record<string, string | undefined>;
|
|
1022
|
+
/**
|
|
1023
|
+
* Custom fetch implementation.
|
|
1024
|
+
*/
|
|
1025
|
+
fetch?: typeof globalThis.fetch;
|
|
1026
|
+
/**
|
|
1027
|
+
* ID generator for tool call fallback IDs.
|
|
1028
|
+
*/
|
|
1029
|
+
generateId?: () => string;
|
|
1030
|
+
/**
|
|
1031
|
+
* Whether to include usage in streaming responses.
|
|
1032
|
+
*/
|
|
1033
|
+
includeUsageInStream?: boolean;
|
|
1034
|
+
/**
|
|
1035
|
+
* Override supported URL patterns.
|
|
1036
|
+
*/
|
|
1037
|
+
supportedUrls?: LanguageModelV3['supportedUrls'];
|
|
1038
|
+
}
|
|
1039
|
+
/**
|
|
1040
|
+
* Reasoning effort levels for extended thinking.
|
|
1041
|
+
* Maps to budget_tokens for the thinking parameter.
|
|
1042
|
+
*
|
|
1043
|
+
* Per Roo Code docs: Enable Reasoning Effort: Medium
|
|
1044
|
+
*/
|
|
1045
|
+
type ReasoningEffort = 'low' | 'medium' | 'high';
|
|
1046
|
+
/**
|
|
1047
|
+
* Configuration for extended thinking/reasoning.
|
|
1048
|
+
* Compatible with Anthropic's thinking parameter format.
|
|
1049
|
+
*
|
|
1050
|
+
* @example
|
|
1051
|
+
* ```ts
|
|
1052
|
+
* // Enable thinking with medium effort
|
|
1053
|
+
* const config: ExtendedThinkingConfig = {
|
|
1054
|
+
* enabled: true,
|
|
1055
|
+
* effort: 'medium'
|
|
1056
|
+
* };
|
|
1057
|
+
*
|
|
1058
|
+
* // Or specify exact budget tokens
|
|
1059
|
+
* const config: ExtendedThinkingConfig = {
|
|
1060
|
+
* enabled: true,
|
|
1061
|
+
* budgetTokens: 10000
|
|
1062
|
+
* };
|
|
1063
|
+
* ```
|
|
1064
|
+
*/
|
|
1065
|
+
interface ExtendedThinkingConfig {
|
|
1066
|
+
/**
|
|
1067
|
+
* Enable extended thinking mode.
|
|
1068
|
+
* @default false
|
|
1069
|
+
*/
|
|
1070
|
+
enabled?: boolean;
|
|
1071
|
+
/**
|
|
1072
|
+
* Reasoning effort level.
|
|
1073
|
+
* Controls how much computation is spent on reasoning.
|
|
1074
|
+
* - low: ~2048 tokens
|
|
1075
|
+
* - medium: ~8192 tokens (default, recommended by Roo Code)
|
|
1076
|
+
* - high: ~16384 tokens
|
|
1077
|
+
* @default 'medium'
|
|
1078
|
+
*/
|
|
1079
|
+
effort?: ReasoningEffort;
|
|
1080
|
+
/**
|
|
1081
|
+
* Budget tokens for thinking (alternative to effort).
|
|
1082
|
+
* Higher values allow for more complex reasoning.
|
|
1083
|
+
* Takes precedence over effort if both are specified.
|
|
1084
|
+
*/
|
|
1085
|
+
budgetTokens?: number;
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
/**
|
|
1089
|
+
* Settings for the Kimi Code provider.
|
|
1090
|
+
*
|
|
1091
|
+
* @remarks
|
|
1092
|
+
* Kimi Code is compatible with Claude Code and Roo Code agents.
|
|
1093
|
+
* Default settings are based on Roo Code documentation:
|
|
1094
|
+
* - Max Output Tokens: 32768
|
|
1095
|
+
* - Context Window Size: 262144
|
|
1096
|
+
* - Reasoning Effort: Medium
|
|
1097
|
+
*
|
|
1098
|
+
* @see https://www.kimi.com/code/docs/en/more/third-party-agents.html
|
|
1099
|
+
* @module
|
|
1100
|
+
*/
|
|
1101
|
+
|
|
1102
|
+
/**
|
|
1103
|
+
* Settings for creating a Kimi Code model instance.
|
|
1104
|
+
*/
|
|
1105
|
+
interface KimiCodeSettings {
|
|
1106
|
+
/**
|
|
1107
|
+
* Override inferred model capabilities.
|
|
1108
|
+
*/
|
|
1109
|
+
capabilities?: KimiCodeCapabilities;
|
|
1110
|
+
/**
|
|
1111
|
+
* Extended thinking/reasoning configuration.
|
|
1112
|
+
* When enabled, the model will show its reasoning process.
|
|
1113
|
+
*
|
|
1114
|
+
* @example
|
|
1115
|
+
* ```ts
|
|
1116
|
+
* const model = kimiCode('kimi-for-coding', {
|
|
1117
|
+
* extendedThinking: {
|
|
1118
|
+
* enabled: true,
|
|
1119
|
+
* effort: 'high'
|
|
1120
|
+
* }
|
|
1121
|
+
* });
|
|
1122
|
+
* ```
|
|
1123
|
+
*/
|
|
1124
|
+
extendedThinking?: ExtendedThinkingConfig | boolean;
|
|
1125
|
+
/**
|
|
1126
|
+
* Whether to include usage in streaming responses.
|
|
1127
|
+
*/
|
|
1128
|
+
includeUsageInStream?: boolean;
|
|
1129
|
+
/**
|
|
1130
|
+
* Override supported URL patterns.
|
|
1131
|
+
*/
|
|
1132
|
+
supportedUrls?: Record<string, RegExp[]>;
|
|
1133
|
+
}
|
|
1134
|
+
/**
|
|
1135
|
+
* Schema for Kimi Code provider options passed via providerOptions.
|
|
1136
|
+
*/
|
|
1137
|
+
declare const kimiCodeProviderOptionsSchema: z.ZodObject<{
|
|
1138
|
+
extendedThinking: z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodObject<{
|
|
1139
|
+
enabled: z.ZodOptional<z.ZodBoolean>;
|
|
1140
|
+
effort: z.ZodOptional<z.ZodEnum<{
|
|
1141
|
+
low: "low";
|
|
1142
|
+
medium: "medium";
|
|
1143
|
+
high: "high";
|
|
1144
|
+
}>>;
|
|
1145
|
+
budgetTokens: z.ZodOptional<z.ZodNumber>;
|
|
1146
|
+
}, z.core.$strip>]>>;
|
|
1147
|
+
system: z.ZodOptional<z.ZodString>;
|
|
1148
|
+
stopSequences: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
1149
|
+
}, z.core.$strip>;
|
|
1150
|
+
/**
|
|
1151
|
+
* Inferred type from the provider options schema.
|
|
1152
|
+
*/
|
|
1153
|
+
type KimiCodeProviderOptions = z.infer<typeof kimiCodeProviderOptionsSchema>;
|
|
1154
|
+
|
|
1155
|
+
/**
|
|
1156
|
+
* Kimi Code provider factory.
|
|
1157
|
+
* @module
|
|
1158
|
+
*/
|
|
1159
|
+
|
|
1160
|
+
/**
|
|
1161
|
+
* Settings for creating a Kimi Code provider instance.
|
|
1162
|
+
*/
|
|
1163
|
+
interface KimiCodeProviderSettings {
|
|
1164
|
+
/**
|
|
1165
|
+
* Kimi Code API key. Defaults to the KIMI_CODE_API_KEY or KIMI_API_KEY environment variable.
|
|
1166
|
+
*/
|
|
1167
|
+
apiKey?: string;
|
|
1168
|
+
/**
|
|
1169
|
+
* Base URL override. Defaults to https://api.kimi.com/coding/v1
|
|
1170
|
+
*/
|
|
1171
|
+
baseURL?: string;
|
|
1172
|
+
/**
|
|
1173
|
+
* Default headers for all requests.
|
|
1174
|
+
*/
|
|
1175
|
+
headers?: Record<string, string | undefined>;
|
|
1176
|
+
/**
|
|
1177
|
+
* Custom fetch implementation.
|
|
1178
|
+
*/
|
|
1179
|
+
fetch?: FetchFunction;
|
|
1180
|
+
/**
|
|
1181
|
+
* ID generator for tool call fallback IDs.
|
|
1182
|
+
*/
|
|
1183
|
+
generateId?: () => string;
|
|
1184
|
+
/**
|
|
1185
|
+
* Include usage details in streaming responses.
|
|
1186
|
+
*/
|
|
1187
|
+
includeUsageInStream?: boolean;
|
|
1188
|
+
/**
|
|
1189
|
+
* Override supported URL patterns for file parts.
|
|
1190
|
+
*/
|
|
1191
|
+
supportedUrls?: LanguageModelV3['supportedUrls'];
|
|
1192
|
+
}
|
|
1193
|
+
/**
|
|
1194
|
+
* The Kimi Code provider interface.
|
|
1195
|
+
*/
|
|
1196
|
+
interface KimiCodeProvider extends Omit<ProviderV3, 'specificationVersion'> {
|
|
1197
|
+
specificationVersion: 'v3';
|
|
1198
|
+
/**
|
|
1199
|
+
* Creates a Kimi Code language model.
|
|
1200
|
+
* @param modelId - The model identifier (defaults to 'kimi-for-coding')
|
|
1201
|
+
* @param settings - Optional model settings
|
|
1202
|
+
*/
|
|
1203
|
+
(modelId?: KimiCodeModelId, settings?: KimiCodeSettings): LanguageModelV3;
|
|
1204
|
+
/**
|
|
1205
|
+
* Creates a Kimi Code language model.
|
|
1206
|
+
* @param modelId - The model identifier
|
|
1207
|
+
* @param settings - Optional model settings
|
|
1208
|
+
*/
|
|
1209
|
+
languageModel(modelId: KimiCodeModelId, settings?: KimiCodeSettings): LanguageModelV3;
|
|
1210
|
+
/**
|
|
1211
|
+
* Creates a Kimi Code language model (alias for languageModel).
|
|
1212
|
+
* @param modelId - The model identifier
|
|
1213
|
+
* @param settings - Optional model settings
|
|
1214
|
+
*/
|
|
1215
|
+
chat(modelId: KimiCodeModelId, settings?: KimiCodeSettings): LanguageModelV3;
|
|
1216
|
+
}
|
|
1217
|
+
/**
|
|
1218
|
+
* Create a Kimi Code provider instance.
|
|
1219
|
+
*
|
|
1220
|
+
* @param options - Provider settings
|
|
1221
|
+
* @returns A configured Kimi Code provider
|
|
1222
|
+
*
|
|
1223
|
+
* @example
|
|
1224
|
+
* ```ts
|
|
1225
|
+
* import { createKimiCode } from 'ai-sdk-provider-kimi';
|
|
1226
|
+
*
|
|
1227
|
+
* const kimiCode = createKimiCode({
|
|
1228
|
+
* apiKey: process.env.KIMI_CODE_API_KEY,
|
|
1229
|
+
* });
|
|
1230
|
+
*
|
|
1231
|
+
* const result = await generateText({
|
|
1232
|
+
* model: kimiCode(), // Uses default 'kimi-for-coding' model
|
|
1233
|
+
* prompt: 'Write a TypeScript function to merge two sorted arrays',
|
|
1234
|
+
* });
|
|
1235
|
+
* ```
|
|
1236
|
+
*
|
|
1237
|
+
* @example
|
|
1238
|
+
* ```ts
|
|
1239
|
+
* // With extended thinking enabled
|
|
1240
|
+
* const result = await generateText({
|
|
1241
|
+
* model: kimiCode('kimi-for-coding', {
|
|
1242
|
+
* extendedThinking: {
|
|
1243
|
+
* enabled: true,
|
|
1244
|
+
* effort: 'high'
|
|
1245
|
+
* }
|
|
1246
|
+
* }),
|
|
1247
|
+
* prompt: 'Design a distributed cache system',
|
|
1248
|
+
* });
|
|
1249
|
+
* ```
|
|
1250
|
+
*
|
|
1251
|
+
* @example
|
|
1252
|
+
* ```ts
|
|
1253
|
+
* // Using with Claude Code compatible settings
|
|
1254
|
+
* const result = await generateText({
|
|
1255
|
+
* model: kimiCode('kimi-k2-thinking'),
|
|
1256
|
+
* prompt: 'Explain and fix this bug',
|
|
1257
|
+
* });
|
|
1258
|
+
* ```
|
|
1259
|
+
*/
|
|
1260
|
+
declare function createKimiCode(options?: KimiCodeProviderSettings): KimiCodeProvider;
|
|
1261
|
+
/**
|
|
1262
|
+
* Default Kimi Code provider instance.
|
|
1263
|
+
*
|
|
1264
|
+
* Uses the KIMI_CODE_API_KEY or KIMI_API_KEY environment variable for authentication.
|
|
1265
|
+
*
|
|
1266
|
+
* @example
|
|
1267
|
+
* ```ts
|
|
1268
|
+
* import { kimiCode } from 'ai-sdk-provider-kimi';
|
|
1269
|
+
*
|
|
1270
|
+
* const result = await generateText({
|
|
1271
|
+
* model: kimiCode(), // Uses default model
|
|
1272
|
+
* prompt: 'Implement a binary search tree',
|
|
1273
|
+
* });
|
|
1274
|
+
* ```
|
|
1275
|
+
*
|
|
1276
|
+
* @example
|
|
1277
|
+
* ```ts
|
|
1278
|
+
* // With extended thinking
|
|
1279
|
+
* const result = await generateText({
|
|
1280
|
+
* model: kimiCode('kimi-for-coding', { extendedThinking: true }),
|
|
1281
|
+
* prompt: 'Design a microservices architecture',
|
|
1282
|
+
* });
|
|
1283
|
+
* ```
|
|
1284
|
+
*/
|
|
1285
|
+
declare const kimiCode: KimiCodeProvider;
|
|
1286
|
+
|
|
1287
|
+
/**
|
|
1288
|
+
* Kimi Code language model implementation.
|
|
1289
|
+
* @module
|
|
1290
|
+
*/
|
|
1291
|
+
|
|
1292
|
+
/**
|
|
1293
|
+
* Kimi Code language model implementing LanguageModelV3.
|
|
1294
|
+
*/
|
|
1295
|
+
declare class KimiCodeLanguageModel implements LanguageModelV3 {
|
|
1296
|
+
readonly specificationVersion = "v3";
|
|
1297
|
+
readonly modelId: KimiCodeModelId;
|
|
1298
|
+
private readonly config;
|
|
1299
|
+
private readonly settings;
|
|
1300
|
+
private readonly generateIdFn;
|
|
1301
|
+
constructor(modelId: KimiCodeModelId, settings: KimiCodeSettings, config: KimiCodeConfig);
|
|
1302
|
+
get provider(): string;
|
|
1303
|
+
private get providerOptionsName();
|
|
1304
|
+
/**
|
|
1305
|
+
* Get the inferred or configured capabilities for this model.
|
|
1306
|
+
*/
|
|
1307
|
+
get capabilities(): KimiCodeCapabilities;
|
|
1308
|
+
get supportedUrls(): Record<string, RegExp[]> | PromiseLike<Record<string, RegExp[]>>;
|
|
1309
|
+
/**
|
|
1310
|
+
* Build request arguments.
|
|
1311
|
+
*/
|
|
1312
|
+
private getArgs;
|
|
1313
|
+
doGenerate(options: LanguageModelV3CallOptions): Promise<LanguageModelV3GenerateResult>;
|
|
1314
|
+
doStream(options: LanguageModelV3CallOptions): Promise<LanguageModelV3StreamResult>;
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
export { type Attachment, type ExtendedThinkingConfig, type FileUploadOptions, type FileUploadResult, KIMI_CODE_BASE_URL, KIMI_CODE_DEFAULT_MODEL, KIMI_CODE_INTERPRETER_TOOL_NAME, KIMI_CODE_THINKING_MODEL, KIMI_WEB_SEARCH_TOOL_NAME, KimiAuthenticationError, type KimiBuiltinTool, type KimiCachingConfig, KimiChatLanguageModel, type KimiChatModelId, type KimiChatSettings, type KimiCodeCapabilities, type KimiCodeInterpreterConfig, type KimiCodeInterpreterToolOptions, KimiCodeLanguageModel, type KimiCodeModelId, type KimiCodeProvider, type KimiCodeProviderOptions, type KimiCodeProviderSettings, type KimiCodeSettings, KimiContentFilterError, KimiContextLengthError, KimiError, type KimiExtendedUsage, type KimiFile, KimiFileClient, type KimiFileClientConfig, type KimiModelCapabilities, KimiModelNotFoundError, type KimiProvider, type KimiProviderOptions, type KimiProviderSettings, KimiRateLimitError, KimiValidationError, type KimiWebSearchConfig, type KimiWebSearchToolConfig, type KimiWebSearchToolOptions, type ProcessedAttachment, type ReasoningEffort, SUPPORTED_FILE_EXTENSIONS, SUPPORTED_MIME_TYPES, createCodeInterpreterTool, createKimi, createKimiCode, createKimiWebSearchTool, createWebSearchTool, getMediaTypeFromExtension, getPurposeFromMediaType, inferKimiCodeCapabilities, inferModelCapabilities, isDocumentMediaType, isFileExtractMediaType, isImageMediaType, isVideoMediaType, kimi, kimiCachingConfigSchema, kimiCode, kimiCodeProviderOptionsSchema, kimiProviderOptionsSchema, kimiTools, processAttachments };
|