kimi-vercel-ai-sdk-provider 0.4.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.
package/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # kimi-vercel-ai-sdk-provider
2
2
 
3
- <img width="1600" height="1278" alt="image" src="https://github.com/user-attachments/assets/fcde2cc0-73e2-4d32-b1bc-b4d9eaca5a6a" />
4
-
5
- Native Kimi (Moonshot AI) provider for Vercel AI SDK.
3
+ <img width="1500" height="762" alt="git" src="https://github.com/user-attachments/assets/5b8d9be4-5eb7-46f3-9096-56bff8dcda67" />
6
4
 
7
5
  This is a native implementation with full support for Kimi-specific features, not a generic OpenAI-compatible wrapper.
8
6
 
@@ -35,6 +33,11 @@ This is a native implementation with full support for Kimi-specific features, no
35
33
  - [Video Input](#video-input-k25-models)
36
34
  - [Model Capabilities](#model-capabilities)
37
35
  - [Advanced Features](#advanced-features)
36
+ - [Auto-Detect Tools](#auto-detect-tools)
37
+ - [Ensemble Generation](#ensemble-generation-multi-sampling)
38
+ - [Code Validation](#code-validation)
39
+ - [Multi-Agent Collaboration](#multi-agent-collaboration)
40
+ - [Project Scaffolding](#project-scaffolding)
38
41
  - [Temperature Locking](#temperature-locking-for-thinking-models)
39
42
  - [File Content Caching](#file-content-caching)
40
43
  - [Schema Sanitization](#schema-sanitization)
@@ -613,6 +616,331 @@ const codeCaps = inferKimiCodeCapabilities('kimi-k2-thinking');
613
616
 
614
617
  ## Advanced Features
615
618
 
619
+ ### Auto-Detect Tools
620
+
621
+ Automatically detect which built-in tools should be enabled based on prompt content:
622
+
623
+ ```ts
624
+ import { kimi, detectToolsFromPrompt, shouldAutoEnableTools } from 'kimi-vercel-ai-sdk-provider';
625
+
626
+ // Simple detection
627
+ const tools = kimi.detectTools('What is the current Bitcoin price?');
628
+ // { webSearch: true, codeInterpreter: false }
629
+
630
+ // Use with model settings
631
+ const model = kimi('kimi-k2.5', {
632
+ webSearch: tools.webSearch,
633
+ codeInterpreter: tools.codeInterpreter
634
+ });
635
+
636
+ // Or use the standalone function with more details
637
+ const result = detectToolsFromPrompt('Calculate the factorial of 20');
638
+ // {
639
+ // webSearch: false,
640
+ // codeInterpreter: true,
641
+ // webSearchConfidence: 0,
642
+ // codeInterpreterConfidence: 0.9,
643
+ // webSearchMatches: [],
644
+ // codeInterpreterMatches: ['calculate']
645
+ // }
646
+
647
+ // Check for opt-outs
648
+ import { hasToolOptOut } from 'kimi-vercel-ai-sdk-provider';
649
+ const optOut = hasToolOptOut("Don't search the web, just answer from memory");
650
+ // { webSearch: true, codeInterpreter: false }
651
+ ```
652
+
653
+ ### Ensemble Generation (Multi-Sampling)
654
+
655
+ Generate multiple responses and select the best one using various strategies:
656
+
657
+ ```ts
658
+ import { kimi, MultiSampler } from 'kimi-vercel-ai-sdk-provider';
659
+ import { generateText } from 'ai';
660
+
661
+ // Using the provider convenience method
662
+ const result = await kimi.ensemble(
663
+ 'Write a function to merge two sorted arrays',
664
+ async (model, prompt, options) => {
665
+ const result = await generateText({
666
+ model,
667
+ prompt,
668
+ temperature: options?.temperature
669
+ });
670
+ return { text: result.text, usage: result.usage };
671
+ },
672
+ {
673
+ n: 3, // Generate 3 samples
674
+ selectionStrategy: 'best', // 'first' | 'vote' | 'best' | 'all'
675
+ scoringHeuristic: 'code', // 'length' | 'confidence' | 'code' | 'custom'
676
+ temperatureVariance: 0.1, // Add variance for diversity
677
+ model: 'kimi-k2.5',
678
+ }
679
+ );
680
+
681
+ console.log(result.text); // Best response
682
+ console.log(result.metadata); // { nRequested: 3, nCompleted: 3, winningIndex: 1, ... }
683
+ console.log(result.alternatives); // All responses (when strategy is 'all')
684
+
685
+ // Or use MultiSampler directly for more control
686
+ const sampler = new MultiSampler({
687
+ generateFn: async (model, prompt, options) => {
688
+ const result = await generateText({ model, prompt, temperature: options?.temperature });
689
+ return { text: result.text };
690
+ },
691
+ modelId: 'kimi-k2.5'
692
+ });
693
+
694
+ const ensembleResult = await sampler.generate(
695
+ kimi('kimi-k2.5'),
696
+ 'Explain quantum computing',
697
+ {
698
+ n: 5,
699
+ selectionStrategy: 'vote', // Majority voting
700
+ timeoutMs: 30000,
701
+ allowPartialFailure: true,
702
+ minSuccessfulSamples: 2
703
+ }
704
+ );
705
+
706
+ // Custom scoring function
707
+ const customResult = await sampler.generate(
708
+ kimi('kimi-k2.5'),
709
+ 'Write clean code',
710
+ {
711
+ n: 3,
712
+ selectionStrategy: 'best',
713
+ scoringHeuristic: 'custom',
714
+ customScorer: (response) => {
715
+ // Higher score = better
716
+ let score = 0;
717
+ if (response.text.includes('```')) score += 10;
718
+ if (response.text.length > 500) score += 5;
719
+ if (!response.text.includes('TODO')) score += 3;
720
+ return score;
721
+ }
722
+ }
723
+ );
724
+ ```
725
+
726
+ ### Code Validation
727
+
728
+ Validate generated code for syntax errors and common issues:
729
+
730
+ ```ts
731
+ import { kimi, CodeValidator, detectLanguage, extractCodeBlocks } from 'kimi-vercel-ai-sdk-provider';
732
+ import { generateText } from 'ai';
733
+
734
+ // Using the provider convenience method
735
+ const result = await kimi.validateCode(
736
+ `function add(a, b) {
737
+ return a + b
738
+ }`,
739
+ async (model, prompt) => {
740
+ const result = await generateText({ model, prompt });
741
+ return { text: result.text };
742
+ },
743
+ {
744
+ language: 'javascript',
745
+ strictness: 'normal', // 'lenient' | 'normal' | 'strict'
746
+ autoFix: true,
747
+ maxAttempts: 3
748
+ }
749
+ );
750
+
751
+ console.log(result.valid); // true/false
752
+ console.log(result.errors); // Array of validation errors
753
+ console.log(result.fixedCode); // Auto-fixed code (if autoFix enabled)
754
+
755
+ // Language detection
756
+ const langResult = detectLanguage(`
757
+ def hello():
758
+ print("Hello, World!")
759
+ `);
760
+ // { language: 'python', confidence: 0.9, indicators: ['def', 'print'] }
761
+
762
+ // Extract code blocks from markdown
763
+ const blocks = extractCodeBlocks(`
764
+ Here's the code:
765
+
766
+ \`\`\`typescript
767
+ const x: number = 42;
768
+ \`\`\`
769
+ `);
770
+ // { blocks: [{ code: 'const x: number = 42;', language: 'typescript', ... }], hasCode: true }
771
+
772
+ // Direct validator usage
773
+ const validator = new CodeValidator({
774
+ generateText: async (prompt) => {
775
+ const result = await generateText({ model: kimi('kimi-k2.5'), prompt });
776
+ return { text: result.text };
777
+ }
778
+ });
779
+
780
+ const validation = await validator.validate(code, {
781
+ language: 'typescript',
782
+ strictness: 'strict',
783
+ autoFix: true,
784
+ validateWithLLM: true, // Use LLM for semantic validation
785
+ maxAttempts: 2
786
+ });
787
+ ```
788
+
789
+ ### Multi-Agent Collaboration
790
+
791
+ Use multiple agents working together for complex tasks:
792
+
793
+ ```ts
794
+ import { kimi, WorkflowRunner, DEFAULT_SYSTEM_PROMPTS } from 'kimi-vercel-ai-sdk-provider';
795
+ import { generateText } from 'ai';
796
+
797
+ // Using the provider convenience method
798
+ const result = await kimi.multiAgent(
799
+ 'Build a REST API for user authentication with JWT',
800
+ async (modelId, prompt, systemPrompt) => {
801
+ const result = await generateText({
802
+ model: kimi(modelId),
803
+ prompt,
804
+ system: systemPrompt
805
+ });
806
+ return { text: result.text, reasoning: result.reasoning };
807
+ },
808
+ {
809
+ workflow: 'planner-executor', // 'planner-executor' | 'proposer-critic' | 'debate' | 'custom'
810
+ modelA: 'kimi-k2.5-thinking', // Planning/thinking agent
811
+ modelB: 'kimi-k2.5', // Execution agent
812
+ iterations: 2,
813
+ validateCode: true,
814
+ verbose: true
815
+ }
816
+ );
817
+
818
+ console.log(result.text); // Final output
819
+ console.log(result.reasoning); // Planning/reasoning output
820
+ console.log(result.intermediateSteps); // All steps in the workflow
821
+ console.log(result.metadata); // { workflow: 'planner-executor', iterations: 2, ... }
822
+
823
+ // Different workflow types:
824
+
825
+ // 1. Planner-Executor: One agent plans, another implements
826
+ const plannerExecutor = await kimi.multiAgent(prompt, generateFn, {
827
+ workflow: 'planner-executor'
828
+ });
829
+
830
+ // 2. Proposer-Critic: Iterative refinement with feedback
831
+ const proposerCritic = await kimi.multiAgent(prompt, generateFn, {
832
+ workflow: 'proposer-critic',
833
+ iterations: 3 // Number of refinement cycles
834
+ });
835
+
836
+ // 3. Debate: Multiple perspectives converge on answer
837
+ const debate = await kimi.multiAgent(prompt, generateFn, {
838
+ workflow: 'debate'
839
+ });
840
+
841
+ // 4. Custom workflow
842
+ const custom = await kimi.multiAgent(prompt, generateFn, {
843
+ workflow: 'custom',
844
+ customWorkflow: async (prompt, context) => {
845
+ // Step 1: Research
846
+ const research = await context.generateWithModelA(
847
+ `Research: ${prompt}`
848
+ );
849
+ context.addStep({ agent: 'A', role: 'custom', action: 'research', output: research.text });
850
+
851
+ // Step 2: Implement
852
+ const implementation = await context.generateWithModelB(
853
+ `Based on research:\n${research.text}\n\nImplement: ${prompt}`
854
+ );
855
+ context.addStep({ agent: 'B', role: 'custom', action: 'implement', output: implementation.text });
856
+
857
+ return {
858
+ text: implementation.text,
859
+ reasoning: research.text,
860
+ intermediateSteps: [], // Filled by context
861
+ usage: { promptTokens: 0, completionTokens: 0, totalTokens: 0 },
862
+ metadata: { workflow: 'custom', iterations: 2, durationMs: 0, models: [], validationEnabled: false, success: true }
863
+ };
864
+ }
865
+ });
866
+
867
+ // Custom system prompts
868
+ const withCustomPrompts = await kimi.multiAgent(prompt, generateFn, {
869
+ workflow: 'proposer-critic',
870
+ systemPrompts: {
871
+ proposer: 'You are a senior software architect. Propose clean, maintainable solutions.',
872
+ critic: 'You are a security expert. Review for vulnerabilities and suggest improvements.'
873
+ }
874
+ });
875
+ ```
876
+
877
+ ### Project Scaffolding
878
+
879
+ Generate complete project structures from descriptions:
880
+
881
+ ```ts
882
+ import { kimi, ProjectScaffolder } from 'kimi-vercel-ai-sdk-provider';
883
+ import { generateText } from 'ai';
884
+
885
+ // Using the provider convenience method
886
+ const result = await kimi.scaffoldProject(
887
+ 'A Next.js app with authentication, database, and API routes',
888
+ async (prompt) => {
889
+ const result = await generateText({ model: kimi('kimi-k2.5'), prompt });
890
+ return { text: result.text };
891
+ },
892
+ {
893
+ type: 'nextjs', // 'auto' | 'nextjs' | 'react' | 'vue' | 'node' | 'express' | 'fastify' | 'python' | 'fastapi' | 'flask' | 'go' | 'rust'
894
+ includeTests: true, // Include test files
895
+ includeCI: true, // Include GitHub Actions
896
+ includeDocs: true, // Include README
897
+ includeDocker: true, // Include Dockerfile
898
+ includeLinting: true, // Include ESLint config
899
+ useTypeScript: true,
900
+ features: ['auth', 'database', 'api'],
901
+ outputFormat: 'files' // 'files' | 'instructions' | 'json'
902
+ }
903
+ );
904
+
905
+ console.log(result.files); // Array of { path, content, description }
906
+ console.log(result.instructions); // Setup instructions markdown
907
+ console.log(result.setupCommands); // ['npm install', 'npm run dev', ...]
908
+ console.log(result.metadata); // { projectType, projectName, fileCount, ... }
909
+
910
+ // Write files to disk
911
+ import { writeFile, mkdir } from 'fs/promises';
912
+ import { dirname, join } from 'path';
913
+
914
+ for (const file of result.files) {
915
+ const filePath = join('./my-project', file.path);
916
+ await mkdir(dirname(filePath), { recursive: true });
917
+ await writeFile(filePath, file.content);
918
+ }
919
+
920
+ // Or use the scaffolder directly
921
+ const scaffolder = new ProjectScaffolder({
922
+ generateText: async (prompt) => {
923
+ const result = await generateText({ model: kimi('kimi-k2.5'), prompt });
924
+ return { text: result.text };
925
+ }
926
+ });
927
+
928
+ const project = await scaffolder.scaffold(
929
+ 'A REST API with Express and MongoDB',
930
+ {
931
+ type: 'express',
932
+ includeTests: true,
933
+ customTemplate: `
934
+ Must include:
935
+ - JWT authentication middleware
936
+ - Request validation with Zod
937
+ - Error handling middleware
938
+ - Rate limiting
939
+ `
940
+ }
941
+ );
942
+ ```
943
+
616
944
  ### Temperature Locking for Thinking Models
617
945
 
618
946
  Thinking models like `kimi-k2.5-thinking` require a fixed temperature of `1.0` for optimal reasoning. The provider automatically enforces this:
@@ -890,8 +1218,11 @@ import {
890
1218
  KimiProviderOptions,
891
1219
  KimiModelCapabilities,
892
1220
  KimiCachingConfig,
893
- } from 'kimi-vercel-ai-sdk-provider
894
- ';
1221
+ EnsembleOptions,
1222
+ MultiAgentOptions,
1223
+ ValidateCodeOptions,
1224
+ ScaffoldProjectOptions,
1225
+ } from 'kimi-vercel-ai-sdk-provider';
895
1226
 
896
1227
  // Kimi Code Provider
897
1228
  import {
@@ -900,16 +1231,10 @@ import {
900
1231
  KimiCodeLanguageModel,
901
1232
  inferKimiCodeCapabilities,
902
1233
  kimiCodeProviderOptionsSchema,
903
- toAnthropicThinking,
904
1234
  // Constants
905
1235
  KIMI_CODE_BASE_URL,
906
- KIMI_CODE_OPENAI_BASE_URL,
907
1236
  KIMI_CODE_DEFAULT_MODEL,
908
1237
  KIMI_CODE_THINKING_MODEL,
909
- KIMI_CODE_MODELS,
910
- KIMI_CODE_DEFAULT_MAX_TOKENS,
911
- KIMI_CODE_DEFAULT_CONTEXT_WINDOW,
912
- KIMI_CODE_ANTHROPIC_VERSION,
913
1238
  // Types
914
1239
  KimiCodeProvider,
915
1240
  KimiCodeProviderSettings,
@@ -924,12 +1249,6 @@ import {
924
1249
  import {
925
1250
  KimiFileClient,
926
1251
  processAttachments,
927
- FileCache,
928
- generateContentHash,
929
- generateCacheKey,
930
- getDefaultFileCache,
931
- setDefaultFileCache,
932
- clearDefaultFileCache,
933
1252
  SUPPORTED_FILE_EXTENSIONS,
934
1253
  SUPPORTED_MIME_TYPES,
935
1254
  isImageMediaType,
@@ -945,20 +1264,87 @@ import {
945
1264
  FileUploadResult,
946
1265
  Attachment,
947
1266
  ProcessedAttachment,
948
- FileCacheOptions,
949
- FileCacheEntry,
950
1267
  } from 'kimi-vercel-ai-sdk-provider';
951
1268
 
952
- // Utilities
1269
+ // Auto-Detect Tools
953
1270
  import {
954
- analyzeReasoningPreservation,
955
- recommendThinkingModel,
956
- // Constants
957
- THINKING_MODEL_TEMPERATURE,
958
- THINKING_MODEL_DEFAULT_MAX_TOKENS,
959
- STANDARD_MODEL_DEFAULT_MAX_TOKENS,
1271
+ detectToolsFromPrompt,
1272
+ shouldAutoEnableTools,
1273
+ hasToolOptOut,
1274
+ generateToolGuidanceMessage,
960
1275
  // Types
961
- ReasoningAnalysis,
1276
+ AutoDetectToolsResult,
1277
+ AutoDetectConfig,
1278
+ ToolGuidanceOptions,
1279
+ } from 'kimi-vercel-ai-sdk-provider';
1280
+
1281
+ // Ensemble / Multi-Sampling
1282
+ import {
1283
+ MultiSampler,
1284
+ createSingletonEnsembleResult,
1285
+ // Types
1286
+ EnsembleConfig,
1287
+ EnsembleResult,
1288
+ EnsembleResponse,
1289
+ EnsembleMetadata,
1290
+ SelectionStrategy,
1291
+ ScoringHeuristic,
1292
+ GenerateFunction,
1293
+ MultiSamplerOptions,
1294
+ } from 'kimi-vercel-ai-sdk-provider';
1295
+
1296
+ // Code Validation
1297
+ import {
1298
+ CodeValidator,
1299
+ detectLanguage,
1300
+ extractCodeBlocks,
1301
+ extractPrimaryCode,
1302
+ containsCode,
1303
+ getFileExtension,
1304
+ createPassedValidationResult,
1305
+ createFailedValidationResult,
1306
+ // Types
1307
+ CodeValidationConfig,
1308
+ ValidationResult,
1309
+ ValidationError,
1310
+ ValidationErrorType,
1311
+ ValidationSeverity,
1312
+ ValidationStrictness,
1313
+ SupportedLanguage,
1314
+ LanguageDetectionResult,
1315
+ CodeBlock,
1316
+ CodeExtractionResult,
1317
+ CodeValidatorOptions,
1318
+ FixAttempt,
1319
+ } from 'kimi-vercel-ai-sdk-provider';
1320
+
1321
+ // Multi-Agent Collaboration
1322
+ import {
1323
+ WorkflowRunner,
1324
+ createEmptyMultiAgentResult,
1325
+ DEFAULT_SYSTEM_PROMPTS,
1326
+ // Types
1327
+ MultiAgentConfig,
1328
+ MultiAgentResult,
1329
+ MultiAgentMetadata,
1330
+ AgentStep,
1331
+ WorkflowContext,
1332
+ WorkflowType,
1333
+ GenerateResult,
1334
+ } from 'kimi-vercel-ai-sdk-provider';
1335
+
1336
+ // Project Scaffolding
1337
+ import {
1338
+ ProjectScaffolder,
1339
+ createEmptyScaffoldResult,
1340
+ // Types
1341
+ ScaffoldConfig,
1342
+ ScaffoldResult,
1343
+ ProjectFile,
1344
+ ProjectMetadata,
1345
+ ProjectType,
1346
+ ProjectTemplate,
1347
+ OutputFormat,
962
1348
  } from 'kimi-vercel-ai-sdk-provider';
963
1349
 
964
1350
  // Built-in Tools
@@ -968,8 +1354,13 @@ import {
968
1354
  createCodeInterpreterTool,
969
1355
  KIMI_WEB_SEARCH_TOOL_NAME,
970
1356
  KIMI_CODE_INTERPRETER_TOOL_NAME,
971
- } from 'kimi-vercel-ai-sdk-provider
972
- ';
1357
+ // Types
1358
+ KimiBuiltinTool,
1359
+ KimiWebSearchConfig,
1360
+ KimiWebSearchToolOptions,
1361
+ KimiCodeInterpreterConfig,
1362
+ KimiCodeInterpreterToolOptions,
1363
+ } from 'kimi-vercel-ai-sdk-provider';
973
1364
 
974
1365
  // Errors
975
1366
  import {
@@ -980,8 +1371,12 @@ import {
980
1371
  KimiContextLengthError,
981
1372
  KimiContentFilterError,
982
1373
  KimiModelNotFoundError,
983
- } from 'kimi-vercel-ai-sdk-provider
984
- ';
1374
+ KimiEnsembleValidationError,
1375
+ KimiEnsembleTimeoutError,
1376
+ KimiMultiAgentError,
1377
+ KimiCodeValidationError,
1378
+ KimiScaffoldError,
1379
+ } from 'kimi-vercel-ai-sdk-provider';
985
1380
  ```
986
1381
 
987
1382
  ### Feature Comparison