kimi-vercel-ai-sdk-provider 0.3.0 → 0.5.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.
Files changed (44) hide show
  1. package/README.md +567 -17
  2. package/dist/index.d.mts +1750 -3
  3. package/dist/index.d.ts +1750 -3
  4. package/dist/index.js +2317 -161
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +2292 -160
  7. package/dist/index.mjs.map +1 -1
  8. package/package.json +1 -1
  9. package/src/__tests__/auto-detect.test.ts +140 -0
  10. package/src/__tests__/code-validation.test.ts +267 -0
  11. package/src/__tests__/ensemble.test.ts +242 -0
  12. package/src/__tests__/file-cache.test.ts +310 -0
  13. package/src/__tests__/model-config.test.ts +120 -0
  14. package/src/__tests__/multi-agent.test.ts +201 -0
  15. package/src/__tests__/project-tools.test.ts +181 -0
  16. package/src/__tests__/reasoning-utils.test.ts +164 -0
  17. package/src/__tests__/tools.test.ts +76 -8
  18. package/src/chat/kimi-chat-language-model.ts +21 -2
  19. package/src/chat/kimi-chat-settings.ts +15 -1
  20. package/src/code-validation/detector.ts +319 -0
  21. package/src/code-validation/index.ts +31 -0
  22. package/src/code-validation/types.ts +291 -0
  23. package/src/code-validation/validator.ts +547 -0
  24. package/src/core/errors.ts +91 -0
  25. package/src/core/index.ts +15 -3
  26. package/src/core/types.ts +57 -2
  27. package/src/core/utils.ts +138 -0
  28. package/src/ensemble/index.ts +17 -0
  29. package/src/ensemble/multi-sampler.ts +433 -0
  30. package/src/ensemble/types.ts +279 -0
  31. package/src/files/attachment-processor.ts +51 -4
  32. package/src/files/file-cache.ts +260 -0
  33. package/src/files/index.ts +16 -1
  34. package/src/index.ts +102 -3
  35. package/src/kimi-provider.ts +354 -1
  36. package/src/multi-agent/index.ts +21 -0
  37. package/src/multi-agent/types.ts +312 -0
  38. package/src/multi-agent/workflows.ts +539 -0
  39. package/src/project-tools/index.ts +16 -0
  40. package/src/project-tools/scaffolder.ts +494 -0
  41. package/src/project-tools/types.ts +244 -0
  42. package/src/tools/auto-detect.ts +276 -0
  43. package/src/tools/index.ts +6 -2
  44. package/src/tools/prepare-tools.ts +179 -4
package/src/index.ts CHANGED
@@ -14,7 +14,15 @@
14
14
  // Kimi Provider (Standard API)
15
15
  // ============================================================================
16
16
 
17
- export type { KimiProvider, KimiProviderSettings } from './kimi-provider';
17
+ export type {
18
+ EnsembleOptions,
19
+ KimiProvider,
20
+ KimiProviderSettings,
21
+ MultiAgentOptions,
22
+ ProviderGenerateFunction,
23
+ ScaffoldProjectOptions,
24
+ ValidateCodeOptions
25
+ } from './kimi-provider';
18
26
  export { createKimi, kimi } from './kimi-provider';
19
27
 
20
28
  // ============================================================================
@@ -91,12 +99,15 @@ export {
91
99
  // ============================================================================
92
100
 
93
101
  export type {
102
+ AutoDetectConfig,
103
+ AutoDetectToolsResult,
94
104
  KimiBuiltinTool,
95
105
  KimiCodeInterpreterConfig,
96
106
  KimiCodeInterpreterToolOptions,
97
107
  KimiWebSearchConfig,
98
108
  KimiWebSearchToolConfig,
99
- KimiWebSearchToolOptions
109
+ KimiWebSearchToolOptions,
110
+ ToolGuidanceOptions
100
111
  } from './tools';
101
112
  export {
102
113
  KIMI_CODE_INTERPRETER_TOOL_NAME,
@@ -104,19 +115,107 @@ export {
104
115
  createCodeInterpreterTool,
105
116
  createKimiWebSearchTool,
106
117
  createWebSearchTool,
107
- kimiTools
118
+ detectToolsFromPrompt,
119
+ generateToolGuidanceMessage,
120
+ hasToolOptOut,
121
+ kimiTools,
122
+ shouldAutoEnableTools
108
123
  } from './tools';
109
124
 
125
+ // ============================================================================
126
+ // Ensemble / Multi-Sampling
127
+ // ============================================================================
128
+
129
+ export type {
130
+ EnsembleConfig,
131
+ EnsembleMetadata,
132
+ EnsembleResponse,
133
+ EnsembleResult,
134
+ GenerateFunction,
135
+ MultiSamplerOptions,
136
+ ScoringHeuristic,
137
+ SelectionStrategy
138
+ } from './ensemble';
139
+ export { MultiSampler, createSingletonEnsembleResult } from './ensemble';
140
+
141
+ // ============================================================================
142
+ // Code Validation
143
+ // ============================================================================
144
+
145
+ export type {
146
+ CodeBlock,
147
+ CodeExtractionResult,
148
+ CodeValidationConfig,
149
+ CodeValidatorOptions,
150
+ FixAttempt,
151
+ LanguageDetectionResult,
152
+ SupportedLanguage,
153
+ ValidationError,
154
+ ValidationErrorType,
155
+ ValidationResult,
156
+ ValidationSeverity,
157
+ ValidationStrictness
158
+ } from './code-validation';
159
+ export {
160
+ CodeValidator,
161
+ containsCode,
162
+ createFailedValidationResult,
163
+ createPassedValidationResult,
164
+ detectLanguage,
165
+ extractCodeBlocks,
166
+ extractPrimaryCode,
167
+ getFileExtension
168
+ } from './code-validation';
169
+
170
+ // ============================================================================
171
+ // Multi-Agent Collaboration
172
+ // ============================================================================
173
+
174
+ export type {
175
+ AgentStep,
176
+ GenerateResult,
177
+ MultiAgentConfig,
178
+ MultiAgentMetadata,
179
+ MultiAgentResult,
180
+ WorkflowContext,
181
+ WorkflowType
182
+ } from './multi-agent';
183
+ export {
184
+ DEFAULT_SYSTEM_PROMPTS,
185
+ WorkflowRunner,
186
+ createEmptyMultiAgentResult
187
+ } from './multi-agent';
188
+
189
+ // ============================================================================
190
+ // Project Scaffolding
191
+ // ============================================================================
192
+
193
+ export type {
194
+ OutputFormat,
195
+ ProjectFile,
196
+ ProjectMetadata,
197
+ ProjectTemplate,
198
+ ProjectType,
199
+ ScaffoldConfig,
200
+ ScaffoldResult
201
+ } from './project-tools';
202
+ export { ProjectScaffolder, createEmptyScaffoldResult } from './project-tools';
203
+
110
204
  // ============================================================================
111
205
  // Errors
112
206
  // ============================================================================
113
207
 
114
208
  export {
115
209
  KimiAuthenticationError,
210
+ KimiCodeValidationError,
116
211
  KimiContentFilterError,
117
212
  KimiContextLengthError,
213
+ KimiEnsembleTimeoutError,
214
+ KimiEnsembleValidationError,
118
215
  KimiError,
119
216
  KimiModelNotFoundError,
217
+ KimiMultiAgentError,
120
218
  KimiRateLimitError,
219
+ KimiScaffoldError,
121
220
  KimiValidationError
122
221
  } from './core';
@@ -3,6 +3,11 @@
3
3
  * @module
4
4
  */
5
5
 
6
+ import type { ValidationResult } from './code-validation';
7
+ import type { EnsembleConfig, GenerateFunction as EnsembleGenerateFunction, EnsembleResult } from './ensemble';
8
+ import type { LanguageModelUsage as EnsembleUsage } from './ensemble/types';
9
+ import type { MultiAgentConfig, MultiAgentResult } from './multi-agent';
10
+ import type { ScaffoldConfig, ScaffoldResult } from './project-tools';
6
11
  import { type LanguageModelV3, NoSuchModelError, type ProviderV3 } from '@ai-sdk/provider';
7
12
  import {
8
13
  type FetchFunction,
@@ -12,8 +17,12 @@ import {
12
17
  withoutTrailingSlash
13
18
  } from '@ai-sdk/provider-utils';
14
19
  import { KimiChatLanguageModel, type KimiChatModelId, type KimiChatSettings } from './chat';
20
+ import { CodeValidator } from './code-validation';
21
+ import { MultiSampler } from './ensemble';
15
22
  import { KimiFileClient } from './files';
16
- import { kimiTools } from './tools';
23
+ import { type GenerateTextFunction as WorkflowGenerateTextFunction, WorkflowRunner } from './multi-agent';
24
+ import { ProjectScaffolder } from './project-tools';
25
+ import { detectToolsFromPrompt, kimiTools } from './tools';
17
26
  import { VERSION } from './version';
18
27
 
19
28
  // ============================================================================
@@ -85,6 +94,115 @@ export interface KimiProviderSettings {
85
94
  // Provider Interface
86
95
  // ============================================================================
87
96
 
97
+ /**
98
+ * Generate function signature for ensemble and convenience methods.
99
+ * Takes a model and prompt and returns generation results.
100
+ */
101
+ export type ProviderGenerateFunction = (
102
+ model: LanguageModelV3,
103
+ prompt: string,
104
+ options?: { temperature?: number }
105
+ ) => Promise<{
106
+ text: string;
107
+ reasoning?: string;
108
+ toolCalls?: unknown[];
109
+ toolResults?: unknown[];
110
+ usage?: EnsembleUsage;
111
+ finishReason?: string;
112
+ }>;
113
+
114
+ /**
115
+ * Options for ensemble generation.
116
+ */
117
+ export interface EnsembleOptions extends Partial<EnsembleConfig> {
118
+ /**
119
+ * Model to use for generation. Defaults to 'kimi-k2.5'.
120
+ */
121
+ model?: KimiChatModelId;
122
+
123
+ /**
124
+ * Base temperature for generation.
125
+ * @default 0.7
126
+ */
127
+ baseTemperature?: number;
128
+ }
129
+
130
+ /**
131
+ * Options for multi-agent workflows.
132
+ */
133
+ export interface MultiAgentOptions extends Partial<MultiAgentConfig> {
134
+ /**
135
+ * Default model settings to apply.
136
+ */
137
+ modelSettings?: KimiChatSettings;
138
+ }
139
+
140
+ /**
141
+ * Options for code validation.
142
+ */
143
+ export interface ValidateCodeOptions {
144
+ /**
145
+ * Model to use for LLM-based validation. Defaults to 'kimi-k2.5'.
146
+ */
147
+ model?: KimiChatModelId;
148
+
149
+ /**
150
+ * Model settings to apply.
151
+ */
152
+ modelSettings?: KimiChatSettings;
153
+
154
+ /**
155
+ * Maximum number of attempts to fix errors.
156
+ * @default 3
157
+ */
158
+ maxAttempts?: number;
159
+
160
+ /**
161
+ * Language to validate (auto-detected if not specified).
162
+ * @default 'auto'
163
+ */
164
+ language?: 'javascript' | 'typescript' | 'python' | 'java' | 'cpp' | 'go' | 'rust' | 'ruby' | 'php' | 'auto';
165
+
166
+ /**
167
+ * Validation strictness level.
168
+ * @default 'strict'
169
+ */
170
+ strictness?: 'lenient' | 'strict' | 'maximum';
171
+
172
+ /**
173
+ * Timeout for each code execution attempt (ms).
174
+ * @default 30000
175
+ */
176
+ executionTimeoutMs?: number;
177
+
178
+ /**
179
+ * Whether to include test cases in validation.
180
+ * @default true
181
+ */
182
+ includeTests?: boolean;
183
+
184
+ /**
185
+ * Whether to return the fixed code even if validation fails.
186
+ * @default true
187
+ */
188
+ returnPartialFix?: boolean;
189
+ }
190
+
191
+ /**
192
+ * Options for project scaffolding.
193
+ */
194
+ export interface ScaffoldProjectOptions extends ScaffoldConfig {
195
+ /**
196
+ * Model to use for generation. Defaults to 'kimi-k2.5'.
197
+ */
198
+ model?: KimiChatModelId;
199
+
200
+ /**
201
+ * Model settings to apply.
202
+ */
203
+ modelSettings?: KimiChatSettings;
204
+ }
205
+
88
206
  /**
89
207
  * The Kimi provider interface.
90
208
  */
@@ -132,6 +250,127 @@ export interface KimiProvider extends Omit<ProviderV3, 'specificationVersion'> {
132
250
  * ```
133
251
  */
134
252
  files: KimiFileClient;
253
+
254
+ /**
255
+ * Generate multiple samples and select the best one using ensemble techniques.
256
+ *
257
+ * @param prompt - The prompt to generate from
258
+ * @param generateFn - Function that generates text (from AI SDK)
259
+ * @param options - Ensemble configuration options
260
+ * @returns The best response based on the selection strategy
261
+ *
262
+ * @example
263
+ * ```ts
264
+ * import { generateText } from 'ai';
265
+ *
266
+ * const result = await kimi.ensemble(
267
+ * 'Write a function to sort an array',
268
+ * async (model, prompt, opts) => {
269
+ * const result = await generateText({ model, prompt, temperature: opts?.temperature });
270
+ * return { text: result.text, usage: result.usage };
271
+ * },
272
+ * { n: 3, selectionStrategy: 'best', scoringHeuristic: 'code' }
273
+ * );
274
+ * ```
275
+ */
276
+ ensemble(prompt: string, generateFn: ProviderGenerateFunction, options?: EnsembleOptions): Promise<EnsembleResult>;
277
+
278
+ /**
279
+ * Run a multi-agent workflow for complex tasks.
280
+ *
281
+ * @param prompt - The task description
282
+ * @param generateFn - Function that generates text (from AI SDK)
283
+ * @param options - Multi-agent workflow configuration
284
+ * @returns The result of the multi-agent collaboration
285
+ *
286
+ * @example
287
+ * ```ts
288
+ * const result = await kimi.multiAgent(
289
+ * 'Build a REST API for user authentication',
290
+ * async (modelId, prompt) => {
291
+ * const result = await generateText({ model: kimi(modelId), prompt });
292
+ * return { text: result.text };
293
+ * },
294
+ * { workflow: 'planner-executor' }
295
+ * );
296
+ * ```
297
+ */
298
+ multiAgent(
299
+ prompt: string,
300
+ generateFn: WorkflowGenerateTextFunction,
301
+ options?: MultiAgentOptions
302
+ ): Promise<MultiAgentResult>;
303
+
304
+ /**
305
+ * Validate code for syntax errors and common issues.
306
+ *
307
+ * @param code - The code to validate
308
+ * @param generateFn - Optional function for LLM-based validation
309
+ * @param options - Validation configuration
310
+ * @returns Validation result with errors and optionally fixed code
311
+ *
312
+ * @example
313
+ * ```ts
314
+ * const result = await kimi.validateCode(
315
+ * 'function test() { return 42 }',
316
+ * async (model, prompt) => {
317
+ * const result = await generateText({ model, prompt });
318
+ * return { text: result.text };
319
+ * },
320
+ * { language: 'javascript', strictness: 'strict' }
321
+ * );
322
+ * ```
323
+ */
324
+ validateCode(
325
+ code: string,
326
+ generateFn?: (model: LanguageModelV3, prompt: string) => Promise<{ text: string }>,
327
+ options?: ValidateCodeOptions
328
+ ): Promise<ValidationResult>;
329
+
330
+ /**
331
+ * Generate a complete project scaffold from a description.
332
+ *
333
+ * @param description - Description of the project to create
334
+ * @param generateFn - Function that generates text (from AI SDK)
335
+ * @param options - Scaffold configuration
336
+ * @returns Generated project files and setup instructions
337
+ *
338
+ * @example
339
+ * ```ts
340
+ * const result = await kimi.scaffoldProject(
341
+ * 'A Next.js app with authentication and database',
342
+ * async (prompt) => {
343
+ * const result = await generateText({ model: kimi('kimi-k2.5'), prompt });
344
+ * return { text: result.text };
345
+ * },
346
+ * { type: 'nextjs', includeTests: true, includeDocker: true }
347
+ * );
348
+ * ```
349
+ */
350
+ scaffoldProject(
351
+ description: string,
352
+ generateFn: (prompt: string) => Promise<{ text: string }>,
353
+ options?: ScaffoldProjectOptions
354
+ ): Promise<ScaffoldResult>;
355
+
356
+ /**
357
+ * Auto-detect which tools should be enabled based on prompt content.
358
+ *
359
+ * @param prompt - The user's prompt
360
+ * @returns Object with webSearch and codeInterpreter booleans
361
+ *
362
+ * @example
363
+ * ```ts
364
+ * const tools = kimi.detectTools('What is the current Bitcoin price?');
365
+ * // { webSearch: true, codeInterpreter: false }
366
+ *
367
+ * const model = kimi('kimi-k2.5', {
368
+ * webSearch: tools.webSearch,
369
+ * codeInterpreter: tools.codeInterpreter
370
+ * });
371
+ * ```
372
+ */
373
+ detectTools(prompt: string): { webSearch: boolean; codeInterpreter: boolean };
135
374
  }
136
375
 
137
376
  // ============================================================================
@@ -241,6 +480,120 @@ export function createKimi(options: KimiProviderSettings = {}): KimiProvider {
241
480
  throw new NoSuchModelError({ modelId, modelType: 'rerankingModel' });
242
481
  };
243
482
 
483
+ // ============================================================================
484
+ // Advanced Feature Methods
485
+ // ============================================================================
486
+
487
+ provider.ensemble = async (
488
+ prompt: string,
489
+ generateFn: ProviderGenerateFunction,
490
+ ensembleOptions: EnsembleOptions = {}
491
+ ): Promise<EnsembleResult> => {
492
+ const { model = 'kimi-k2.5', baseTemperature = 0.7, ...config } = ensembleOptions;
493
+
494
+ const sampler = new MultiSampler({
495
+ modelId: model,
496
+ baseTemperature
497
+ });
498
+
499
+ // Wrap the generate function to match the expected signature
500
+ const wrappedGenerateFn: EnsembleGenerateFunction = async (options) => {
501
+ const languageModel = createChatModel(model, {});
502
+ const result = await generateFn(languageModel, prompt, { temperature: options.temperature });
503
+ return {
504
+ text: result.text,
505
+ reasoning: result.reasoning,
506
+ toolCalls: result.toolCalls,
507
+ toolResults: result.toolResults,
508
+ usage: result.usage,
509
+ finishReason: result.finishReason ?? 'stop'
510
+ };
511
+ };
512
+
513
+ return sampler.generate(wrappedGenerateFn, {
514
+ n: config.n ?? 3,
515
+ selectionStrategy: config.selectionStrategy ?? 'best',
516
+ temperatureVariance: config.temperatureVariance ?? 0.1,
517
+ scoringHeuristic: config.scoringHeuristic ?? 'confidence',
518
+ customScorer: config.customScorer,
519
+ timeoutMs: config.timeoutMs ?? 60000,
520
+ allowPartialFailure: config.allowPartialFailure ?? true,
521
+ minSuccessfulSamples: config.minSuccessfulSamples ?? 1
522
+ });
523
+ };
524
+
525
+ provider.multiAgent = async (
526
+ prompt: string,
527
+ generateFn: WorkflowGenerateTextFunction,
528
+ agentOptions: MultiAgentOptions = {}
529
+ ): Promise<MultiAgentResult> => {
530
+ const { modelSettings, ...config } = agentOptions;
531
+
532
+ const runner = new WorkflowRunner(generateFn);
533
+
534
+ return runner.run(prompt, {
535
+ workflow: config.workflow ?? 'planner-executor',
536
+ modelA: config.modelA ?? 'kimi-k2.5-thinking',
537
+ modelB: config.modelB ?? 'kimi-k2.5',
538
+ iterations: config.iterations ?? 2,
539
+ validateCode: config.validateCode ?? false,
540
+ timeoutMs: config.timeoutMs ?? 120000,
541
+ customWorkflow: config.customWorkflow,
542
+ verbose: config.verbose ?? false,
543
+ systemPrompts: config.systemPrompts
544
+ });
545
+ };
546
+
547
+ provider.validateCode = async (
548
+ code: string,
549
+ generateFn?: (model: LanguageModelV3, prompt: string) => Promise<{ text: string }>,
550
+ validateOptions: ValidateCodeOptions = {}
551
+ ): Promise<ValidationResult> => {
552
+ const { model = 'kimi-k2.5', modelSettings, ...config } = validateOptions;
553
+
554
+ // Create generate function that uses our model if LLM validation is needed
555
+ const languageModel = createChatModel(model, modelSettings);
556
+ const llmGenerateFn = generateFn
557
+ ? async (prompt: string) => generateFn(languageModel, prompt)
558
+ : async (_prompt: string) => {
559
+ return { text: '' };
560
+ }; // Fallback for static-only validation
561
+
562
+ const validator = new CodeValidator({
563
+ generateText: llmGenerateFn
564
+ });
565
+
566
+ return validator.validate(code, {
567
+ enabled: true,
568
+ maxAttempts: config.maxAttempts ?? 3,
569
+ language: config.language ?? 'auto',
570
+ strictness: config.strictness ?? 'strict',
571
+ executionTimeoutMs: config.executionTimeoutMs ?? 30000,
572
+ includeTests: config.includeTests ?? true,
573
+ returnPartialFix: config.returnPartialFix ?? true
574
+ });
575
+ };
576
+
577
+ provider.scaffoldProject = async (
578
+ description: string,
579
+ generateFn: (prompt: string) => Promise<{ text: string }>,
580
+ scaffoldOptions: ScaffoldProjectOptions = {}
581
+ ): Promise<ScaffoldResult> => {
582
+ const scaffolder = new ProjectScaffolder({
583
+ generateText: generateFn
584
+ });
585
+
586
+ return scaffolder.scaffold(description, scaffoldOptions);
587
+ };
588
+
589
+ provider.detectTools = (prompt: string): { webSearch: boolean; codeInterpreter: boolean } => {
590
+ const result = detectToolsFromPrompt(prompt);
591
+ return {
592
+ webSearch: result.webSearch,
593
+ codeInterpreter: result.codeInterpreter
594
+ };
595
+ };
596
+
244
597
  return provider;
245
598
  }
246
599
 
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Multi-agent collaboration module exports.
3
+ * @module
4
+ */
5
+
6
+ export type {
7
+ AgentStep,
8
+ GenerateResult,
9
+ MultiAgentConfig,
10
+ MultiAgentMetadata,
11
+ MultiAgentResult,
12
+ WorkflowContext,
13
+ WorkflowType
14
+ } from './types';
15
+ export type {
16
+ GenerateTextFunction,
17
+ ValidateCodeFunction,
18
+ WorkflowRunnerOptions
19
+ } from './workflows';
20
+ export { DEFAULT_SYSTEM_PROMPTS } from './types';
21
+ export { WorkflowRunner, createEmptyMultiAgentResult } from './workflows';