File size: 10,542 Bytes
4e23b01 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | import type { KimiConfig } from '#/index';
import { describe, expect, it, vi } from 'vitest';
import {
applyCatalogProvider,
catalogModelToAlias,
catalogProviderModels,
CatalogFetchError,
fetchCatalog,
loadBuiltInCatalog,
type CatalogModel,
} from '../src/catalog';
function catalogResponse(body: unknown, status = 200): Response {
return new Response(JSON.stringify(body), {
status,
headers: { 'Content-Type': 'application/json' },
});
}
const model: CatalogModel = {
id: 'm1',
name: 'M1',
maxOutputSize: 64000,
capability: {
image_in: true,
video_in: false,
audio_in: false,
thinking: true,
tool_use: true,
max_context_tokens: 200000,
},
};
describe('fetchCatalog', () => {
it('fetches and returns the catalog map', async () => {
const catalog = { anthropic: { id: 'anthropic', models: { x: { id: 'x', limit: { context: 1000 } } } } };
const fetchMock = vi.fn(async () => catalogResponse(catalog));
const result = await fetchCatalog('https://x/api.json', {
fetchImpl: fetchMock as unknown as typeof fetch,
});
expect(result).toEqual(catalog);
});
it('throws CatalogFetchError on HTTP error', async () => {
const fetchMock = vi.fn(async () => catalogResponse('no', 500));
await expect(
fetchCatalog('https://x', { fetchImpl: fetchMock as unknown as typeof fetch }),
).rejects.toBeInstanceOf(CatalogFetchError);
});
it('throws on a non-object payload', async () => {
const fetchMock = vi.fn(async () => catalogResponse([1, 2]));
await expect(
fetchCatalog('https://x', { fetchImpl: fetchMock as unknown as typeof fetch }),
).rejects.toThrow(/Unexpected catalog response/);
});
it('sends the given User-Agent, and none by default', async () => {
const fetchMock = vi.fn(async () => catalogResponse({}));
await fetchCatalog(
'https://x/api.json',
{
fetchImpl: fetchMock as unknown as typeof fetch,
userAgent: 'kimi-code-cli/1.2.3',
},
);
const withUa = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
const withUaHeaders = withUa[1].headers as Record<string, string>;
expect(withUaHeaders['User-Agent']).toBe('kimi-code-cli/1.2.3');
expect(withUaHeaders['Accept']).toBe('application/json');
fetchMock.mockClear();
await fetchCatalog('https://x/api.json', {
fetchImpl: fetchMock as unknown as typeof fetch,
});
const withoutUa = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
expect((withoutUa[1].headers as Record<string, string>)['User-Agent']).toBeUndefined();
});
});
describe('loadBuiltInCatalog', () => {
it('parses a valid catalog JSON string', () => {
const catalog = { anthropic: { id: 'anthropic', models: {} } };
expect(loadBuiltInCatalog(JSON.stringify(catalog))).toEqual(catalog);
});
it('returns undefined for missing or invalid input', () => {
expect(loadBuiltInCatalog(undefined)).toBeUndefined();
expect(loadBuiltInCatalog('')).toBeUndefined();
expect(loadBuiltInCatalog('not-json')).toBeUndefined();
});
});
describe('catalogModelToAlias', () => {
it('flattens a catalog model capability into alias fields', () => {
expect(catalogModelToAlias('anthropic', model)).toEqual({
provider: 'anthropic',
model: 'm1',
maxContextSize: 200000,
maxOutputSize: 64000,
capabilities: ['image_in', 'thinking', 'tool_use'],
displayName: 'M1',
});
});
});
describe('applyCatalogProvider', () => {
it('writes provider, model aliases, and defaults', () => {
const config = { providers: {} } as KimiConfig;
const result = applyCatalogProvider(config, {
providerId: 'anthropic',
wire: 'anthropic',
baseUrl: 'https://api.anthropic.com',
apiKey: 'sk',
models: [model],
selectedModelId: 'm1',
thinking: true,
});
expect(result.defaultModel).toBe('anthropic/m1');
expect(config.providers['anthropic']).toMatchObject({ type: 'anthropic', apiKey: 'sk' });
expect(config.models?.['anthropic/m1']).toMatchObject({
provider: 'anthropic',
model: 'm1',
maxContextSize: 200000,
});
expect(config.defaultModel).toBe('anthropic/m1');
expect(config.thinking?.enabled).toBe(true);
});
it('writes interleaved reasoning key from a catalog-selected model alias', () => {
const models = catalogProviderModels({
id: 'deepseek',
models: {
'deepseek-v4-pro': {
id: 'deepseek-v4-pro',
name: 'DeepSeek V4 Pro',
family: 'deepseek-thinking',
limit: { context: 1000000, output: 384000 },
reasoning: true,
tool_call: true,
interleaved: { field: 'reasoning_content' },
},
},
});
const config = { providers: {} } as KimiConfig;
applyCatalogProvider(config, {
providerId: 'deepseek',
wire: 'openai',
baseUrl: 'https://api.deepseek.com',
apiKey: 'sk',
models,
selectedModelId: 'deepseek-v4-pro',
thinking: true,
});
expect(config.models?.['deepseek/deepseek-v4-pro']).toMatchObject({
provider: 'deepseek',
model: 'deepseek-v4-pro',
reasoningKey: 'reasoning_content',
});
});
it('writes declared effort levels from reasoning_options into the model alias', () => {
// The models.dev `kimi-for-coding` provider shape for `k3`.
const models = catalogProviderModels({
id: 'kimi-for-coding',
models: {
k3: {
id: 'k3',
name: 'Kimi K3',
limit: { context: 1048576, output: 131072 },
reasoning: true,
reasoning_options: [
{ type: 'toggle' },
{ type: 'effort', values: ['low', 'high', 'max'] },
],
tool_call: true,
modalities: { input: ['text', 'image', 'video'], output: ['text'] },
},
},
});
const config = { providers: {} } as KimiConfig;
applyCatalogProvider(config, {
providerId: 'kimi-for-coding',
wire: 'anthropic',
baseUrl: 'https://api.kimi.com/coding',
apiKey: 'sk',
models,
selectedModelId: 'k3',
thinking: true,
});
expect(config.models?.['kimi-for-coding/k3']).toMatchObject({
provider: 'kimi-for-coding',
model: 'k3',
capabilities: ['image_in', 'video_in', 'thinking', 'tool_use'],
supportEfforts: ['low', 'high', 'max'],
});
});
it('writes per-model protocol/baseUrl overrides and the input-limited context size', () => {
// The zenmux gateway shape: provider defaults to the OpenAI wire, one
// model is served over Anthropic on its own endpoint; plus a gpt-5-style
// input cap below the total context window.
const models = catalogProviderModels({
id: 'gateway',
npm: '@ai-sdk/openai-compatible',
api: 'https://gateway.example.test/api/v1',
models: {
'vendor/claude-model': {
id: 'vendor/claude-model',
name: 'Gateway Claude',
limit: { context: 200000 },
provider: {
npm: '@ai-sdk/anthropic',
api: 'https://gateway.example.test/api/anthropic/v1',
},
},
'vendor/gpt-model': {
id: 'vendor/gpt-model',
limit: { context: 400000, input: 272000, output: 128000 },
},
},
});
const config = { providers: {} } as KimiConfig;
applyCatalogProvider(config, {
providerId: 'gateway',
wire: 'openai',
baseUrl: 'https://gateway.example.test/api/v1',
apiKey: 'sk',
models,
selectedModelId: 'vendor/claude-model',
thinking: false,
});
expect(config.models?.['gateway/vendor/claude-model']).toMatchObject({
provider: 'gateway',
model: 'vendor/claude-model',
protocol: 'anthropic',
baseUrl: 'https://gateway.example.test/api/anthropic',
});
const plain = config.models?.['gateway/vendor/gpt-model'];
expect(plain).toMatchObject({ maxContextSize: 400000, maxInputSize: 272000 });
expect(plain?.protocol).toBeUndefined();
expect(plain?.baseUrl).toBeUndefined();
});
it('maps always-thinking models to always_thinking and carries the off encoding', () => {
const models = catalogProviderModels({
id: 'gateway',
models: {
'gpt-5': {
id: 'gpt-5',
reasoning: true,
reasoning_options: [{ type: 'effort', values: ['low', 'medium', 'high'] }],
limit: { context: 400000, input: 272000 },
},
'grok-4': {
id: 'grok-4',
reasoning: true,
reasoning_options: [{ type: 'effort', values: ['none', 'low', 'medium', 'high'] }],
limit: { context: 256000 },
},
},
});
const config = { providers: {} } as KimiConfig;
applyCatalogProvider(config, {
providerId: 'gateway',
wire: 'openai',
baseUrl: 'https://gateway.example.test/v1',
apiKey: 'sk',
models,
selectedModelId: 'gpt-5',
thinking: true,
});
// No off option: thinking is locked on for a model that always reasons.
expect(config.models?.['gateway/gpt-5']).toMatchObject({
capabilities: ['thinking', 'tool_use'].map((c) => (c === 'thinking' ? 'always_thinking' : c)),
supportEfforts: ['low', 'medium', 'high'],
});
expect(config.models?.['gateway/gpt-5']?.capabilities).not.toContain('thinking');
expect(config.models?.['gateway/gpt-5']?.offEffort).toBeUndefined();
// 'none' becomes the off encoding; the level list stays selectable-only.
expect(config.models?.['gateway/grok-4']).toMatchObject({
capabilities: ['thinking', 'tool_use'],
supportEfforts: ['low', 'medium', 'high'],
offEffort: 'none',
});
});
it('clears stale aliases for the same provider but keeps others', () => {
const config = {
providers: { anthropic: { type: 'anthropic', apiKey: 'old' } },
models: {
'anthropic/stale': { provider: 'anthropic', model: 'stale', maxContextSize: 1 },
'other/keep': { provider: 'other', model: 'keep', maxContextSize: 1 },
},
} as unknown as KimiConfig;
applyCatalogProvider(config, {
providerId: 'anthropic',
wire: 'anthropic',
apiKey: 'new',
models: [model],
selectedModelId: 'm1',
thinking: false,
});
expect(config.models?.['anthropic/stale']).toBeUndefined();
expect(config.models?.['other/keep']).toBeDefined();
});
});
|