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
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for file content caching.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
6
|
+
import {
|
|
7
|
+
FileCache,
|
|
8
|
+
type FileCacheEntry,
|
|
9
|
+
clearDefaultFileCache,
|
|
10
|
+
generateCacheKey,
|
|
11
|
+
generateContentHash,
|
|
12
|
+
getDefaultFileCache,
|
|
13
|
+
setDefaultFileCache
|
|
14
|
+
} from '../files';
|
|
15
|
+
|
|
16
|
+
describe('FileCache', () => {
|
|
17
|
+
describe('basic operations', () => {
|
|
18
|
+
it('should store and retrieve entries', () => {
|
|
19
|
+
const cache = new FileCache();
|
|
20
|
+
const entry: FileCacheEntry = {
|
|
21
|
+
fileId: 'file_123',
|
|
22
|
+
content: 'extracted text',
|
|
23
|
+
createdAt: Date.now(),
|
|
24
|
+
purpose: 'file-extract'
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
cache.set('hash123', entry);
|
|
28
|
+
const retrieved = cache.get('hash123');
|
|
29
|
+
|
|
30
|
+
expect(retrieved).toEqual(entry);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should return undefined for missing entries', () => {
|
|
34
|
+
const cache = new FileCache();
|
|
35
|
+
|
|
36
|
+
expect(cache.get('nonexistent')).toBeUndefined();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should delete entries', () => {
|
|
40
|
+
const cache = new FileCache();
|
|
41
|
+
const entry: FileCacheEntry = {
|
|
42
|
+
fileId: 'file_123',
|
|
43
|
+
createdAt: Date.now(),
|
|
44
|
+
purpose: 'file-extract'
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
cache.set('hash123', entry);
|
|
48
|
+
expect(cache.has('hash123')).toBe(true);
|
|
49
|
+
|
|
50
|
+
cache.delete('hash123');
|
|
51
|
+
expect(cache.has('hash123')).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('should clear all entries', () => {
|
|
55
|
+
const cache = new FileCache();
|
|
56
|
+
|
|
57
|
+
cache.set('hash1', { fileId: 'f1', createdAt: Date.now(), purpose: 'file-extract' });
|
|
58
|
+
cache.set('hash2', { fileId: 'f2', createdAt: Date.now(), purpose: 'image' });
|
|
59
|
+
cache.set('hash3', { fileId: 'f3', createdAt: Date.now(), purpose: 'video' });
|
|
60
|
+
|
|
61
|
+
expect(cache.size).toBe(3);
|
|
62
|
+
|
|
63
|
+
cache.clear();
|
|
64
|
+
expect(cache.size).toBe(0);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should report correct size', () => {
|
|
68
|
+
const cache = new FileCache();
|
|
69
|
+
|
|
70
|
+
expect(cache.size).toBe(0);
|
|
71
|
+
|
|
72
|
+
cache.set('hash1', { fileId: 'f1', createdAt: Date.now(), purpose: 'file-extract' });
|
|
73
|
+
expect(cache.size).toBe(1);
|
|
74
|
+
|
|
75
|
+
cache.set('hash2', { fileId: 'f2', createdAt: Date.now(), purpose: 'file-extract' });
|
|
76
|
+
expect(cache.size).toBe(2);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe('LRU eviction', () => {
|
|
81
|
+
it('should evict oldest entries when at capacity', () => {
|
|
82
|
+
const cache = new FileCache({ maxSize: 3 });
|
|
83
|
+
|
|
84
|
+
cache.set('hash1', { fileId: 'f1', createdAt: Date.now(), purpose: 'file-extract' });
|
|
85
|
+
cache.set('hash2', { fileId: 'f2', createdAt: Date.now(), purpose: 'file-extract' });
|
|
86
|
+
cache.set('hash3', { fileId: 'f3', createdAt: Date.now(), purpose: 'file-extract' });
|
|
87
|
+
|
|
88
|
+
expect(cache.size).toBe(3);
|
|
89
|
+
|
|
90
|
+
// Add a 4th entry, should evict hash1
|
|
91
|
+
cache.set('hash4', { fileId: 'f4', createdAt: Date.now(), purpose: 'file-extract' });
|
|
92
|
+
|
|
93
|
+
expect(cache.size).toBe(3);
|
|
94
|
+
expect(cache.has('hash1')).toBe(false);
|
|
95
|
+
expect(cache.has('hash2')).toBe(true);
|
|
96
|
+
expect(cache.has('hash3')).toBe(true);
|
|
97
|
+
expect(cache.has('hash4')).toBe(true);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should update LRU order on get', () => {
|
|
101
|
+
const cache = new FileCache({ maxSize: 3 });
|
|
102
|
+
|
|
103
|
+
cache.set('hash1', { fileId: 'f1', createdAt: Date.now(), purpose: 'file-extract' });
|
|
104
|
+
cache.set('hash2', { fileId: 'f2', createdAt: Date.now(), purpose: 'file-extract' });
|
|
105
|
+
cache.set('hash3', { fileId: 'f3', createdAt: Date.now(), purpose: 'file-extract' });
|
|
106
|
+
|
|
107
|
+
// Access hash1 to make it recently used
|
|
108
|
+
cache.get('hash1');
|
|
109
|
+
|
|
110
|
+
// Add a 4th entry, should evict hash2 (not hash1)
|
|
111
|
+
cache.set('hash4', { fileId: 'f4', createdAt: Date.now(), purpose: 'file-extract' });
|
|
112
|
+
|
|
113
|
+
expect(cache.has('hash1')).toBe(true);
|
|
114
|
+
expect(cache.has('hash2')).toBe(false);
|
|
115
|
+
expect(cache.has('hash3')).toBe(true);
|
|
116
|
+
expect(cache.has('hash4')).toBe(true);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('should handle updating existing entries', () => {
|
|
120
|
+
const cache = new FileCache({ maxSize: 3 });
|
|
121
|
+
|
|
122
|
+
cache.set('hash1', { fileId: 'f1', createdAt: Date.now(), purpose: 'file-extract' });
|
|
123
|
+
cache.set('hash2', { fileId: 'f2', createdAt: Date.now(), purpose: 'file-extract' });
|
|
124
|
+
cache.set('hash3', { fileId: 'f3', createdAt: Date.now(), purpose: 'file-extract' });
|
|
125
|
+
|
|
126
|
+
// Update hash1
|
|
127
|
+
cache.set('hash1', { fileId: 'f1-updated', createdAt: Date.now(), purpose: 'file-extract' });
|
|
128
|
+
|
|
129
|
+
expect(cache.size).toBe(3);
|
|
130
|
+
expect(cache.get('hash1')?.fileId).toBe('f1-updated');
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe('TTL expiration', () => {
|
|
135
|
+
beforeEach(() => {
|
|
136
|
+
vi.useFakeTimers();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
afterEach(() => {
|
|
140
|
+
vi.useRealTimers();
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('should expire entries after TTL', () => {
|
|
144
|
+
const cache = new FileCache({ ttlMs: 1000 }); // 1 second TTL
|
|
145
|
+
|
|
146
|
+
cache.set('hash1', { fileId: 'f1', createdAt: Date.now(), purpose: 'file-extract' });
|
|
147
|
+
|
|
148
|
+
expect(cache.get('hash1')).toBeDefined();
|
|
149
|
+
|
|
150
|
+
// Advance time past TTL
|
|
151
|
+
vi.advanceTimersByTime(1500);
|
|
152
|
+
|
|
153
|
+
expect(cache.get('hash1')).toBeUndefined();
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('should not expire entries before TTL', () => {
|
|
157
|
+
const cache = new FileCache({ ttlMs: 1000 });
|
|
158
|
+
|
|
159
|
+
cache.set('hash1', { fileId: 'f1', createdAt: Date.now(), purpose: 'file-extract' });
|
|
160
|
+
|
|
161
|
+
// Advance time but not past TTL
|
|
162
|
+
vi.advanceTimersByTime(500);
|
|
163
|
+
|
|
164
|
+
expect(cache.get('hash1')).toBeDefined();
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('should prune expired entries', () => {
|
|
168
|
+
const cache = new FileCache({ ttlMs: 1000 });
|
|
169
|
+
|
|
170
|
+
cache.set('hash1', { fileId: 'f1', createdAt: Date.now(), purpose: 'file-extract' });
|
|
171
|
+
cache.set('hash2', { fileId: 'f2', createdAt: Date.now(), purpose: 'file-extract' });
|
|
172
|
+
|
|
173
|
+
vi.advanceTimersByTime(1500);
|
|
174
|
+
|
|
175
|
+
cache.set('hash3', { fileId: 'f3', createdAt: Date.now(), purpose: 'file-extract' });
|
|
176
|
+
|
|
177
|
+
const pruned = cache.prune();
|
|
178
|
+
|
|
179
|
+
expect(pruned).toBe(2);
|
|
180
|
+
expect(cache.size).toBe(1);
|
|
181
|
+
expect(cache.has('hash3')).toBe(true);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
describe('default options', () => {
|
|
186
|
+
it('should use default maxSize of 100', () => {
|
|
187
|
+
const cache = new FileCache();
|
|
188
|
+
|
|
189
|
+
// Add 100 entries
|
|
190
|
+
for (let i = 0; i < 100; i++) {
|
|
191
|
+
cache.set(`hash${i}`, { fileId: `f${i}`, createdAt: Date.now(), purpose: 'file-extract' });
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
expect(cache.size).toBe(100);
|
|
195
|
+
|
|
196
|
+
// Add one more, should evict
|
|
197
|
+
cache.set('hash100', { fileId: 'f100', createdAt: Date.now(), purpose: 'file-extract' });
|
|
198
|
+
expect(cache.size).toBe(100);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('should use default TTL of 1 hour', () => {
|
|
202
|
+
vi.useFakeTimers();
|
|
203
|
+
|
|
204
|
+
const cache = new FileCache();
|
|
205
|
+
cache.set('hash1', { fileId: 'f1', createdAt: Date.now(), purpose: 'file-extract' });
|
|
206
|
+
|
|
207
|
+
// 59 minutes - should still be valid
|
|
208
|
+
vi.advanceTimersByTime(59 * 60 * 1000);
|
|
209
|
+
expect(cache.get('hash1')).toBeDefined();
|
|
210
|
+
|
|
211
|
+
// 61 minutes - should be expired
|
|
212
|
+
vi.advanceTimersByTime(2 * 60 * 1000);
|
|
213
|
+
expect(cache.get('hash1')).toBeUndefined();
|
|
214
|
+
|
|
215
|
+
vi.useRealTimers();
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
describe('generateContentHash', () => {
|
|
221
|
+
it('should generate consistent hashes for same content', () => {
|
|
222
|
+
const data = new Uint8Array([1, 2, 3, 4, 5]);
|
|
223
|
+
|
|
224
|
+
const hash1 = generateContentHash(data);
|
|
225
|
+
const hash2 = generateContentHash(data);
|
|
226
|
+
|
|
227
|
+
expect(hash1).toBe(hash2);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it('should generate different hashes for different content', () => {
|
|
231
|
+
const data1 = new Uint8Array([1, 2, 3]);
|
|
232
|
+
const data2 = new Uint8Array([4, 5, 6]);
|
|
233
|
+
|
|
234
|
+
const hash1 = generateContentHash(data1);
|
|
235
|
+
const hash2 = generateContentHash(data2);
|
|
236
|
+
|
|
237
|
+
expect(hash1).not.toBe(hash2);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it('should work with string input', () => {
|
|
241
|
+
const hash1 = generateContentHash('hello world');
|
|
242
|
+
const hash2 = generateContentHash('hello world');
|
|
243
|
+
const hash3 = generateContentHash('goodbye world');
|
|
244
|
+
|
|
245
|
+
expect(hash1).toBe(hash2);
|
|
246
|
+
expect(hash1).not.toBe(hash3);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it('should return 8-character hex string', () => {
|
|
250
|
+
const hash = generateContentHash('test');
|
|
251
|
+
|
|
252
|
+
expect(hash).toMatch(/^[0-9a-f]{8}$/);
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
describe('generateCacheKey', () => {
|
|
257
|
+
it('should include content hash, size, and filename', () => {
|
|
258
|
+
const data = new Uint8Array([1, 2, 3, 4, 5]);
|
|
259
|
+
const key = generateCacheKey(data, 'document.pdf');
|
|
260
|
+
|
|
261
|
+
expect(key).toContain('_5_'); // size
|
|
262
|
+
expect(key).toContain('document.pdf');
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it('should normalize filename', () => {
|
|
266
|
+
const data = new Uint8Array([1, 2, 3]);
|
|
267
|
+
const key = generateCacheKey(data, 'My Document (1).PDF');
|
|
268
|
+
|
|
269
|
+
expect(key).toContain('my_document__1_.pdf');
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it('should generate different keys for same content with different names', () => {
|
|
273
|
+
const data = new Uint8Array([1, 2, 3, 4, 5]);
|
|
274
|
+
|
|
275
|
+
const key1 = generateCacheKey(data, 'file1.pdf');
|
|
276
|
+
const key2 = generateCacheKey(data, 'file2.pdf');
|
|
277
|
+
|
|
278
|
+
expect(key1).not.toBe(key2);
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
describe('Default Cache', () => {
|
|
283
|
+
afterEach(() => {
|
|
284
|
+
clearDefaultFileCache();
|
|
285
|
+
setDefaultFileCache(null);
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it('should create default cache on first access', () => {
|
|
289
|
+
const cache1 = getDefaultFileCache();
|
|
290
|
+
const cache2 = getDefaultFileCache();
|
|
291
|
+
|
|
292
|
+
expect(cache1).toBe(cache2);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('should allow setting custom default cache', () => {
|
|
296
|
+
const customCache = new FileCache({ maxSize: 10 });
|
|
297
|
+
setDefaultFileCache(customCache);
|
|
298
|
+
|
|
299
|
+
expect(getDefaultFileCache()).toBe(customCache);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it('should clear default cache', () => {
|
|
303
|
+
const cache = getDefaultFileCache();
|
|
304
|
+
cache.set('test', { fileId: 'f1', createdAt: Date.now(), purpose: 'file-extract' });
|
|
305
|
+
|
|
306
|
+
clearDefaultFileCache();
|
|
307
|
+
|
|
308
|
+
expect(cache.size).toBe(0);
|
|
309
|
+
});
|
|
310
|
+
});
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for model configuration features:
|
|
3
|
+
* - Temperature locking for thinking models
|
|
4
|
+
* - Default max_tokens
|
|
5
|
+
* - Model capability inference
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, expect, it } from 'vitest';
|
|
9
|
+
import {
|
|
10
|
+
STANDARD_MODEL_DEFAULT_MAX_TOKENS,
|
|
11
|
+
THINKING_MODEL_DEFAULT_MAX_TOKENS,
|
|
12
|
+
THINKING_MODEL_TEMPERATURE,
|
|
13
|
+
inferModelCapabilities
|
|
14
|
+
} from '../core';
|
|
15
|
+
|
|
16
|
+
describe('Model Configuration', () => {
|
|
17
|
+
describe('inferModelCapabilities', () => {
|
|
18
|
+
it('should detect thinking models by suffix', () => {
|
|
19
|
+
const caps = inferModelCapabilities('kimi-k2.5-thinking');
|
|
20
|
+
|
|
21
|
+
expect(caps.thinking).toBe(true);
|
|
22
|
+
expect(caps.alwaysThinking).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should detect non-thinking models', () => {
|
|
26
|
+
const caps = inferModelCapabilities('kimi-k2.5');
|
|
27
|
+
|
|
28
|
+
expect(caps.thinking).toBe(false);
|
|
29
|
+
expect(caps.alwaysThinking).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should detect K2.5 models for video support', () => {
|
|
33
|
+
const k25Caps = inferModelCapabilities('kimi-k2.5');
|
|
34
|
+
const k2Caps = inferModelCapabilities('kimi-k2-turbo');
|
|
35
|
+
|
|
36
|
+
expect(k25Caps.videoInput).toBe(true);
|
|
37
|
+
expect(k2Caps.videoInput).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should support alternative K2.5 naming', () => {
|
|
41
|
+
const caps = inferModelCapabilities('kimi-k2-5-thinking');
|
|
42
|
+
|
|
43
|
+
expect(caps.videoInput).toBe(true);
|
|
44
|
+
expect(caps.thinking).toBe(true);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('Temperature Locking', () => {
|
|
49
|
+
it('should set locked temperature for thinking models', () => {
|
|
50
|
+
const caps = inferModelCapabilities('kimi-k2.5-thinking');
|
|
51
|
+
|
|
52
|
+
expect(caps.temperatureLocked).toBe(true);
|
|
53
|
+
expect(caps.defaultTemperature).toBe(THINKING_MODEL_TEMPERATURE);
|
|
54
|
+
expect(caps.defaultTemperature).toBe(1.0);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should not lock temperature for standard models', () => {
|
|
58
|
+
const caps = inferModelCapabilities('kimi-k2.5');
|
|
59
|
+
|
|
60
|
+
expect(caps.temperatureLocked).toBe(false);
|
|
61
|
+
expect(caps.defaultTemperature).toBeUndefined();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('should use correct constant value', () => {
|
|
65
|
+
expect(THINKING_MODEL_TEMPERATURE).toBe(1.0);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe('Default Max Tokens', () => {
|
|
70
|
+
it('should set higher default for thinking models', () => {
|
|
71
|
+
const caps = inferModelCapabilities('kimi-k2.5-thinking');
|
|
72
|
+
|
|
73
|
+
expect(caps.defaultMaxOutputTokens).toBe(THINKING_MODEL_DEFAULT_MAX_TOKENS);
|
|
74
|
+
expect(caps.defaultMaxOutputTokens).toBe(32768);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should set standard default for regular models', () => {
|
|
78
|
+
const caps = inferModelCapabilities('kimi-k2.5');
|
|
79
|
+
|
|
80
|
+
expect(caps.defaultMaxOutputTokens).toBe(STANDARD_MODEL_DEFAULT_MAX_TOKENS);
|
|
81
|
+
expect(caps.defaultMaxOutputTokens).toBe(4096);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should use correct constant values', () => {
|
|
85
|
+
expect(THINKING_MODEL_DEFAULT_MAX_TOKENS).toBe(32768);
|
|
86
|
+
expect(STANDARD_MODEL_DEFAULT_MAX_TOKENS).toBe(4096);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe('All models have common capabilities', () => {
|
|
91
|
+
const testModels = ['kimi-k2.5', 'kimi-k2.5-thinking', 'kimi-k2-turbo', 'kimi-k2-thinking'];
|
|
92
|
+
|
|
93
|
+
for (const modelId of testModels) {
|
|
94
|
+
it(`${modelId} should have imageInput support`, () => {
|
|
95
|
+
const caps = inferModelCapabilities(modelId);
|
|
96
|
+
expect(caps.imageInput).toBe(true);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it(`${modelId} should have 256k context`, () => {
|
|
100
|
+
const caps = inferModelCapabilities(modelId);
|
|
101
|
+
expect(caps.maxContextSize).toBe(256_000);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it(`${modelId} should support tool calling`, () => {
|
|
105
|
+
const caps = inferModelCapabilities(modelId);
|
|
106
|
+
expect(caps.toolCalling).toBe(true);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it(`${modelId} should support JSON mode`, () => {
|
|
110
|
+
const caps = inferModelCapabilities(modelId);
|
|
111
|
+
expect(caps.jsonMode).toBe(true);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it(`${modelId} should support structured outputs`, () => {
|
|
115
|
+
const caps = inferModelCapabilities(modelId);
|
|
116
|
+
expect(caps.structuredOutputs).toBe(true);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
});
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { DEFAULT_SYSTEM_PROMPTS, type WorkflowType } from '../multi-agent/types';
|
|
3
|
+
import { WorkflowRunner, createEmptyMultiAgentResult } from '../multi-agent/workflows';
|
|
4
|
+
|
|
5
|
+
describe('Multi-Agent Collaboration', () => {
|
|
6
|
+
const createMockGenerator = (responses: Record<string, string>) =>
|
|
7
|
+
vi.fn().mockImplementation(async (modelId: string) => {
|
|
8
|
+
return {
|
|
9
|
+
text: responses[modelId] || `Response from ${modelId}`,
|
|
10
|
+
usage: { promptTokens: 10, completionTokens: 20, totalTokens: 30 }
|
|
11
|
+
};
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
describe('WorkflowRunner', () => {
|
|
15
|
+
describe('planner-executor workflow', () => {
|
|
16
|
+
it('runs planner-executor workflow', async () => {
|
|
17
|
+
const generator = createMockGenerator({
|
|
18
|
+
'kimi-k2.5-thinking': '1. Analyze requirements\n2. Design solution\n3. Implement',
|
|
19
|
+
'kimi-k2.5': 'function solve() { return 42; }'
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const runner = new WorkflowRunner(generator);
|
|
23
|
+
const result = await runner.run('Create a function', {
|
|
24
|
+
workflow: 'planner-executor',
|
|
25
|
+
modelA: 'kimi-k2.5-thinking',
|
|
26
|
+
modelB: 'kimi-k2.5'
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
expect(result.text).toContain('function solve');
|
|
30
|
+
expect(result.reasoning).toContain('Analyze requirements');
|
|
31
|
+
expect(result.intermediateSteps).toHaveLength(2);
|
|
32
|
+
expect(result.metadata.workflow).toBe('planner-executor');
|
|
33
|
+
expect(result.metadata.models).toContain('kimi-k2.5-thinking');
|
|
34
|
+
expect(result.metadata.models).toContain('kimi-k2.5');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('includes validation step when enabled', async () => {
|
|
38
|
+
const generator = createMockGenerator({
|
|
39
|
+
'kimi-k2.5-thinking': 'Plan step',
|
|
40
|
+
'kimi-k2.5': '```javascript\nconst x = 5;\n```'
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const validateCode = vi.fn().mockResolvedValue({
|
|
44
|
+
valid: true,
|
|
45
|
+
errors: []
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const runner = new WorkflowRunner(generator, validateCode);
|
|
49
|
+
const result = await runner.run('Create code', {
|
|
50
|
+
workflow: 'planner-executor',
|
|
51
|
+
validateCode: true
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
expect(result.intermediateSteps.length).toBeGreaterThanOrEqual(2);
|
|
55
|
+
expect(result.validation).toBeDefined();
|
|
56
|
+
expect(result.validation?.valid).toBe(true);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe('proposer-critic workflow', () => {
|
|
61
|
+
it('runs proposer-critic workflow', async () => {
|
|
62
|
+
const generator = createMockGenerator({
|
|
63
|
+
'kimi-k2.5': 'First attempt at solution',
|
|
64
|
+
'kimi-k2.5-thinking': 'Consider these improvements...'
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const runner = new WorkflowRunner(generator);
|
|
68
|
+
const result = await runner.run('Solve this problem', {
|
|
69
|
+
workflow: 'proposer-critic',
|
|
70
|
+
iterations: 2,
|
|
71
|
+
modelA: 'kimi-k2.5',
|
|
72
|
+
modelB: 'kimi-k2.5-thinking'
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
expect(result.metadata.workflow).toBe('proposer-critic');
|
|
76
|
+
expect(result.intermediateSteps.length).toBeGreaterThanOrEqual(2);
|
|
77
|
+
|
|
78
|
+
const roles = result.intermediateSteps.map((s) => s.role);
|
|
79
|
+
expect(roles).toContain('proposer');
|
|
80
|
+
expect(roles).toContain('critic');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('iterates specified number of times', async () => {
|
|
84
|
+
const generator = vi.fn().mockResolvedValue({
|
|
85
|
+
text: 'Response',
|
|
86
|
+
usage: { promptTokens: 10, completionTokens: 20, totalTokens: 30 }
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const runner = new WorkflowRunner(generator);
|
|
90
|
+
await runner.run('Task', {
|
|
91
|
+
workflow: 'proposer-critic',
|
|
92
|
+
iterations: 3
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// 3 proposer iterations + 2 critic iterations (critic doesn't run on last)
|
|
96
|
+
expect(generator).toHaveBeenCalledTimes(5);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe('debate workflow', () => {
|
|
101
|
+
it('runs debate workflow', async () => {
|
|
102
|
+
const generator = createMockGenerator({
|
|
103
|
+
'kimi-k2.5': 'Position A'
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const runner = new WorkflowRunner(generator);
|
|
107
|
+
const result = await runner.run('Debate this topic', {
|
|
108
|
+
workflow: 'debate',
|
|
109
|
+
iterations: 2
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
expect(result.metadata.workflow).toBe('debate');
|
|
113
|
+
expect(result.intermediateSteps.length).toBeGreaterThan(0);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe('custom workflow', () => {
|
|
118
|
+
it('runs custom workflow', async () => {
|
|
119
|
+
const generator = createMockGenerator({});
|
|
120
|
+
const runner = new WorkflowRunner(generator);
|
|
121
|
+
|
|
122
|
+
const customWorkflow = vi.fn().mockResolvedValue({
|
|
123
|
+
text: 'Custom result',
|
|
124
|
+
intermediateSteps: [
|
|
125
|
+
{
|
|
126
|
+
agent: 'Custom',
|
|
127
|
+
role: 'custom',
|
|
128
|
+
action: 'Custom action',
|
|
129
|
+
output: 'Custom output',
|
|
130
|
+
timestamp: Date.now(),
|
|
131
|
+
durationMs: 100
|
|
132
|
+
}
|
|
133
|
+
],
|
|
134
|
+
usage: { promptTokens: 0, completionTokens: 0, totalTokens: 0 },
|
|
135
|
+
metadata: {
|
|
136
|
+
workflow: 'custom',
|
|
137
|
+
iterations: 1,
|
|
138
|
+
durationMs: 0,
|
|
139
|
+
models: [],
|
|
140
|
+
validationEnabled: false,
|
|
141
|
+
success: true
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
const result = await runner.run('Task', {
|
|
146
|
+
workflow: 'custom',
|
|
147
|
+
customWorkflow
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
expect(customWorkflow).toHaveBeenCalled();
|
|
151
|
+
expect(result.text).toBe('Custom result');
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('throws without customWorkflow function', async () => {
|
|
155
|
+
const generator = createMockGenerator({});
|
|
156
|
+
const runner = new WorkflowRunner(generator);
|
|
157
|
+
|
|
158
|
+
await expect(runner.run('Task', { workflow: 'custom' })).rejects.toThrow(
|
|
159
|
+
'Custom workflow requires customWorkflow function'
|
|
160
|
+
);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
describe('error handling', () => {
|
|
165
|
+
it('throws for unknown workflow', async () => {
|
|
166
|
+
const generator = createMockGenerator({});
|
|
167
|
+
const runner = new WorkflowRunner(generator);
|
|
168
|
+
|
|
169
|
+
await expect(runner.run('Task', { workflow: 'invalid' as WorkflowType })).rejects.toThrow(
|
|
170
|
+
'Unknown workflow type'
|
|
171
|
+
);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
describe('createEmptyMultiAgentResult', () => {
|
|
177
|
+
it('creates empty result with error', () => {
|
|
178
|
+
const result = createEmptyMultiAgentResult('planner-executor', 'Something failed');
|
|
179
|
+
|
|
180
|
+
expect(result.text).toBe('');
|
|
181
|
+
expect(result.intermediateSteps).toHaveLength(0);
|
|
182
|
+
expect(result.metadata.success).toBe(false);
|
|
183
|
+
expect(result.metadata.error).toBe('Something failed');
|
|
184
|
+
expect(result.metadata.workflow).toBe('planner-executor');
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
describe('DEFAULT_SYSTEM_PROMPTS', () => {
|
|
189
|
+
it('has all required prompts', () => {
|
|
190
|
+
expect(DEFAULT_SYSTEM_PROMPTS.planner).toBeDefined();
|
|
191
|
+
expect(DEFAULT_SYSTEM_PROMPTS.executor).toBeDefined();
|
|
192
|
+
expect(DEFAULT_SYSTEM_PROMPTS.proposer).toBeDefined();
|
|
193
|
+
expect(DEFAULT_SYSTEM_PROMPTS.critic).toBeDefined();
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('prompts are non-empty strings', () => {
|
|
197
|
+
expect(typeof DEFAULT_SYSTEM_PROMPTS.planner).toBe('string');
|
|
198
|
+
expect(DEFAULT_SYSTEM_PROMPTS.planner.length).toBeGreaterThan(50);
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
});
|