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.
- package/README.md +567 -17
- package/dist/index.d.mts +1750 -3
- package/dist/index.d.ts +1750 -3
- package/dist/index.js +2317 -161
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2292 -160
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/auto-detect.test.ts +140 -0
- package/src/__tests__/code-validation.test.ts +267 -0
- package/src/__tests__/ensemble.test.ts +242 -0
- package/src/__tests__/file-cache.test.ts +310 -0
- package/src/__tests__/model-config.test.ts +120 -0
- package/src/__tests__/multi-agent.test.ts +201 -0
- package/src/__tests__/project-tools.test.ts +181 -0
- package/src/__tests__/reasoning-utils.test.ts +164 -0
- package/src/__tests__/tools.test.ts +76 -8
- package/src/chat/kimi-chat-language-model.ts +21 -2
- package/src/chat/kimi-chat-settings.ts +15 -1
- package/src/code-validation/detector.ts +319 -0
- package/src/code-validation/index.ts +31 -0
- package/src/code-validation/types.ts +291 -0
- package/src/code-validation/validator.ts +547 -0
- package/src/core/errors.ts +91 -0
- package/src/core/index.ts +15 -3
- package/src/core/types.ts +57 -2
- package/src/core/utils.ts +138 -0
- package/src/ensemble/index.ts +17 -0
- package/src/ensemble/multi-sampler.ts +433 -0
- package/src/ensemble/types.ts +279 -0
- package/src/files/attachment-processor.ts +51 -4
- package/src/files/file-cache.ts +260 -0
- package/src/files/index.ts +16 -1
- package/src/index.ts +102 -3
- package/src/kimi-provider.ts +354 -1
- package/src/multi-agent/index.ts +21 -0
- package/src/multi-agent/types.ts +312 -0
- package/src/multi-agent/workflows.ts +539 -0
- package/src/project-tools/index.ts +16 -0
- package/src/project-tools/scaffolder.ts +494 -0
- package/src/project-tools/types.ts +244 -0
- package/src/tools/auto-detect.ts +276 -0
- package/src/tools/index.ts +6 -2
- package/src/tools/prepare-tools.ts +179 -4
package/README.md
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
# kimi-vercel-ai-sdk-provider
|
|
2
2
|
|
|
3
|
-
<img width="
|
|
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
|
|
|
@@ -34,6 +32,16 @@ This is a native implementation with full support for Kimi-specific features, no
|
|
|
34
32
|
- [Reasoning/Thinking Models](#reasoningthinking-models)
|
|
35
33
|
- [Video Input](#video-input-k25-models)
|
|
36
34
|
- [Model Capabilities](#model-capabilities)
|
|
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)
|
|
41
|
+
- [Temperature Locking](#temperature-locking-for-thinking-models)
|
|
42
|
+
- [File Content Caching](#file-content-caching)
|
|
43
|
+
- [Schema Sanitization](#schema-sanitization)
|
|
44
|
+
- [Reasoning Preservation](#reasoning-preservation-utilities)
|
|
37
45
|
- [Provider Options](#provider-options)
|
|
38
46
|
- [Available Models](#available-models-1)
|
|
39
47
|
- [Regional Endpoints](#regional-endpoints)
|
|
@@ -56,6 +64,9 @@ This is a native implementation with full support for Kimi-specific features, no
|
|
|
56
64
|
- **Native File & PDF Support** - Automatic file upload and content extraction
|
|
57
65
|
- **Tool Choice Polyfill** - Simulates `required` and `tool` choices via system messages
|
|
58
66
|
- **Context Caching** - Reduce costs by up to 90% for repeated long prompts
|
|
67
|
+
- **Temperature Locking** - Automatic temperature enforcement for thinking models
|
|
68
|
+
- **File Content Caching** - LRU cache to avoid re-uploading identical files
|
|
69
|
+
- **Schema Sanitization** - Automatic cleanup of unsupported JSON Schema keywords
|
|
59
70
|
|
|
60
71
|
### Kimi Code (Premium Coding API)
|
|
61
72
|
- High-speed output (up to 100 tokens/s)
|
|
@@ -603,6 +614,459 @@ const codeCaps = inferKimiCodeCapabilities('kimi-k2-thinking');
|
|
|
603
614
|
// }
|
|
604
615
|
```
|
|
605
616
|
|
|
617
|
+
## Advanced Features
|
|
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
|
+
|
|
944
|
+
### Temperature Locking for Thinking Models
|
|
945
|
+
|
|
946
|
+
Thinking models like `kimi-k2.5-thinking` require a fixed temperature of `1.0` for optimal reasoning. The provider automatically enforces this:
|
|
947
|
+
|
|
948
|
+
```ts
|
|
949
|
+
// Temperature is automatically set to 1.0 for thinking models
|
|
950
|
+
const result = await generateText({
|
|
951
|
+
model: kimi('kimi-k2.5-thinking'),
|
|
952
|
+
temperature: 0.7, // Will be ignored with a warning
|
|
953
|
+
prompt: 'Solve this complex problem...',
|
|
954
|
+
});
|
|
955
|
+
|
|
956
|
+
// Check the response for warnings
|
|
957
|
+
console.log(result.warnings);
|
|
958
|
+
// [{ type: 'compatibility', feature: 'temperature', details: 'Thinking models require temperature=1.0...' }]
|
|
959
|
+
```
|
|
960
|
+
|
|
961
|
+
Thinking models also default to 32k max tokens to prevent reasoning truncation:
|
|
962
|
+
|
|
963
|
+
```ts
|
|
964
|
+
// No need to set maxTokens - defaults to 32768 for thinking models
|
|
965
|
+
const result = await generateText({
|
|
966
|
+
model: kimi('kimi-k2.5-thinking'),
|
|
967
|
+
prompt: 'Explain quantum computing in detail...',
|
|
968
|
+
});
|
|
969
|
+
```
|
|
970
|
+
|
|
971
|
+
### File Content Caching
|
|
972
|
+
|
|
973
|
+
Avoid re-uploading the same files by enabling the LRU cache:
|
|
974
|
+
|
|
975
|
+
```ts
|
|
976
|
+
import { processAttachments } from 'kimi-vercel-ai-sdk-provider';
|
|
977
|
+
|
|
978
|
+
// Enable caching (uses default global cache: 100 entries, 1 hour TTL)
|
|
979
|
+
const processed = await processAttachments({
|
|
980
|
+
attachments: message.experimental_attachments ?? [],
|
|
981
|
+
clientConfig: {
|
|
982
|
+
baseURL: 'https://api.moonshot.ai/v1',
|
|
983
|
+
headers: () => ({ Authorization: `Bearer ${process.env.MOONSHOT_API_KEY}` }),
|
|
984
|
+
},
|
|
985
|
+
cache: true, // Enable file caching
|
|
986
|
+
});
|
|
987
|
+
|
|
988
|
+
// Or provide a custom cache instance
|
|
989
|
+
import { FileCache } from 'kimi-vercel-ai-sdk-provider';
|
|
990
|
+
|
|
991
|
+
const customCache = new FileCache({
|
|
992
|
+
maxSize: 200, // Max 200 entries
|
|
993
|
+
ttlMs: 2 * 60 * 60 * 1000, // 2 hour TTL
|
|
994
|
+
});
|
|
995
|
+
|
|
996
|
+
const processed = await processAttachments({
|
|
997
|
+
attachments,
|
|
998
|
+
clientConfig,
|
|
999
|
+
cache: customCache,
|
|
1000
|
+
});
|
|
1001
|
+
```
|
|
1002
|
+
|
|
1003
|
+
### Schema Sanitization
|
|
1004
|
+
|
|
1005
|
+
Tool parameters are automatically sanitized to remove JSON Schema keywords not supported by Kimi:
|
|
1006
|
+
|
|
1007
|
+
```ts
|
|
1008
|
+
// This schema with advanced JSON Schema features...
|
|
1009
|
+
const complexTool = {
|
|
1010
|
+
name: 'search',
|
|
1011
|
+
parameters: z.object({
|
|
1012
|
+
query: z.string(),
|
|
1013
|
+
filters: z.object({
|
|
1014
|
+
$schema: 'http://json-schema.org/draft-07/schema#', // Removed
|
|
1015
|
+
allOf: [{ minLength: 1 }], // Removed
|
|
1016
|
+
anyOf: [{ type: 'string' }], // Removed
|
|
1017
|
+
}),
|
|
1018
|
+
}),
|
|
1019
|
+
};
|
|
1020
|
+
|
|
1021
|
+
// ...is automatically sanitized before being sent to Kimi
|
|
1022
|
+
// Only basic properties (type, properties, required, description) are kept
|
|
1023
|
+
```
|
|
1024
|
+
|
|
1025
|
+
### Reasoning Preservation Utilities
|
|
1026
|
+
|
|
1027
|
+
Helpers for maintaining reasoning context in multi-turn conversations:
|
|
1028
|
+
|
|
1029
|
+
```ts
|
|
1030
|
+
import {
|
|
1031
|
+
analyzeReasoningPreservation,
|
|
1032
|
+
recommendThinkingModel
|
|
1033
|
+
} from 'kimi-vercel-ai-sdk-provider';
|
|
1034
|
+
|
|
1035
|
+
// Analyze if reasoning is properly preserved in a conversation
|
|
1036
|
+
const messages = [
|
|
1037
|
+
{ role: 'user', content: 'Solve this step by step: ...' },
|
|
1038
|
+
{
|
|
1039
|
+
role: 'assistant',
|
|
1040
|
+
content: [
|
|
1041
|
+
{ type: 'reasoning', text: 'First, I need to...' },
|
|
1042
|
+
{ type: 'text', text: 'The answer is 42.' }
|
|
1043
|
+
]
|
|
1044
|
+
},
|
|
1045
|
+
{ role: 'user', content: 'Explain step 2 more.' },
|
|
1046
|
+
];
|
|
1047
|
+
|
|
1048
|
+
const analysis = analyzeReasoningPreservation(messages);
|
|
1049
|
+
// {
|
|
1050
|
+
// hasReasoningContent: true,
|
|
1051
|
+
// reasoningPreserved: true,
|
|
1052
|
+
// turnCount: 3,
|
|
1053
|
+
// reasoningTurnCount: 1,
|
|
1054
|
+
// recommendations: []
|
|
1055
|
+
// }
|
|
1056
|
+
|
|
1057
|
+
// Get a recommendation on whether to use a thinking model
|
|
1058
|
+
const recommendation = recommendThinkingModel({
|
|
1059
|
+
taskDescription: 'Complex mathematical proof',
|
|
1060
|
+
requiresStepByStep: true,
|
|
1061
|
+
complexity: 'high',
|
|
1062
|
+
});
|
|
1063
|
+
// {
|
|
1064
|
+
// recommended: true,
|
|
1065
|
+
// reason: 'Task requires step-by-step reasoning with high complexity',
|
|
1066
|
+
// suggestedModel: 'kimi-k2.5-thinking'
|
|
1067
|
+
// }
|
|
1068
|
+
```
|
|
1069
|
+
|
|
606
1070
|
## Provider Options
|
|
607
1071
|
|
|
608
1072
|
### Kimi Chat Options
|
|
@@ -754,8 +1218,11 @@ import {
|
|
|
754
1218
|
KimiProviderOptions,
|
|
755
1219
|
KimiModelCapabilities,
|
|
756
1220
|
KimiCachingConfig,
|
|
757
|
-
|
|
758
|
-
|
|
1221
|
+
EnsembleOptions,
|
|
1222
|
+
MultiAgentOptions,
|
|
1223
|
+
ValidateCodeOptions,
|
|
1224
|
+
ScaffoldProjectOptions,
|
|
1225
|
+
} from 'kimi-vercel-ai-sdk-provider';
|
|
759
1226
|
|
|
760
1227
|
// Kimi Code Provider
|
|
761
1228
|
import {
|
|
@@ -764,16 +1231,10 @@ import {
|
|
|
764
1231
|
KimiCodeLanguageModel,
|
|
765
1232
|
inferKimiCodeCapabilities,
|
|
766
1233
|
kimiCodeProviderOptionsSchema,
|
|
767
|
-
toAnthropicThinking,
|
|
768
1234
|
// Constants
|
|
769
1235
|
KIMI_CODE_BASE_URL,
|
|
770
|
-
KIMI_CODE_OPENAI_BASE_URL,
|
|
771
1236
|
KIMI_CODE_DEFAULT_MODEL,
|
|
772
1237
|
KIMI_CODE_THINKING_MODEL,
|
|
773
|
-
KIMI_CODE_MODELS,
|
|
774
|
-
KIMI_CODE_DEFAULT_MAX_TOKENS,
|
|
775
|
-
KIMI_CODE_DEFAULT_CONTEXT_WINDOW,
|
|
776
|
-
KIMI_CODE_ANTHROPIC_VERSION,
|
|
777
1238
|
// Types
|
|
778
1239
|
KimiCodeProvider,
|
|
779
1240
|
KimiCodeProviderSettings,
|
|
@@ -803,8 +1264,88 @@ import {
|
|
|
803
1264
|
FileUploadResult,
|
|
804
1265
|
Attachment,
|
|
805
1266
|
ProcessedAttachment,
|
|
806
|
-
} from 'kimi-vercel-ai-sdk-provider
|
|
807
|
-
|
|
1267
|
+
} from 'kimi-vercel-ai-sdk-provider';
|
|
1268
|
+
|
|
1269
|
+
// Auto-Detect Tools
|
|
1270
|
+
import {
|
|
1271
|
+
detectToolsFromPrompt,
|
|
1272
|
+
shouldAutoEnableTools,
|
|
1273
|
+
hasToolOptOut,
|
|
1274
|
+
generateToolGuidanceMessage,
|
|
1275
|
+
// Types
|
|
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,
|
|
1348
|
+
} from 'kimi-vercel-ai-sdk-provider';
|
|
808
1349
|
|
|
809
1350
|
// Built-in Tools
|
|
810
1351
|
import {
|
|
@@ -813,8 +1354,13 @@ import {
|
|
|
813
1354
|
createCodeInterpreterTool,
|
|
814
1355
|
KIMI_WEB_SEARCH_TOOL_NAME,
|
|
815
1356
|
KIMI_CODE_INTERPRETER_TOOL_NAME,
|
|
816
|
-
|
|
817
|
-
|
|
1357
|
+
// Types
|
|
1358
|
+
KimiBuiltinTool,
|
|
1359
|
+
KimiWebSearchConfig,
|
|
1360
|
+
KimiWebSearchToolOptions,
|
|
1361
|
+
KimiCodeInterpreterConfig,
|
|
1362
|
+
KimiCodeInterpreterToolOptions,
|
|
1363
|
+
} from 'kimi-vercel-ai-sdk-provider';
|
|
818
1364
|
|
|
819
1365
|
// Errors
|
|
820
1366
|
import {
|
|
@@ -825,8 +1371,12 @@ import {
|
|
|
825
1371
|
KimiContextLengthError,
|
|
826
1372
|
KimiContentFilterError,
|
|
827
1373
|
KimiModelNotFoundError,
|
|
828
|
-
|
|
829
|
-
|
|
1374
|
+
KimiEnsembleValidationError,
|
|
1375
|
+
KimiEnsembleTimeoutError,
|
|
1376
|
+
KimiMultiAgentError,
|
|
1377
|
+
KimiCodeValidationError,
|
|
1378
|
+
KimiScaffoldError,
|
|
1379
|
+
} from 'kimi-vercel-ai-sdk-provider';
|
|
830
1380
|
```
|
|
831
1381
|
|
|
832
1382
|
### Feature Comparison
|