|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Qwen |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, expect, it, vi } from 'vitest'; |
| 8 | +import type OpenAI from 'openai'; |
| 9 | +import type { Config } from '../../../config/config.js'; |
| 10 | +import type { ContentGeneratorConfig } from '../../contentGenerator.js'; |
| 11 | +import { determineProvider } from '../index.js'; |
| 12 | + |
| 13 | +function createCliConfig(): Config { |
| 14 | + return { |
| 15 | + getCliVersion: vi.fn().mockReturnValue('1.0.0'), |
| 16 | + getProxy: vi.fn().mockReturnValue(undefined), |
| 17 | + } as unknown as Config; |
| 18 | +} |
| 19 | + |
| 20 | +function createProviderConfig( |
| 21 | + overrides: Partial<ContentGeneratorConfig>, |
| 22 | +): ContentGeneratorConfig { |
| 23 | + return { |
| 24 | + apiKey: 'test-api-key', |
| 25 | + baseUrl: 'https://api.mistral.ai/v1', |
| 26 | + model: 'mistral-large-latest', |
| 27 | + ...overrides, |
| 28 | + } as ContentGeneratorConfig; |
| 29 | +} |
| 30 | + |
| 31 | +function createReasoningRequest(): OpenAI.Chat.ChatCompletionCreateParams { |
| 32 | + return { |
| 33 | + model: 'mistral-large-latest', |
| 34 | + messages: [ |
| 35 | + { role: 'user', content: 'Say OK' }, |
| 36 | + { |
| 37 | + role: 'assistant', |
| 38 | + content: 'OK', |
| 39 | + reasoning_content: 'User asked for a short response.', |
| 40 | + } as OpenAI.Chat.ChatCompletionAssistantMessageParam & { |
| 41 | + reasoning_content: string; |
| 42 | + }, |
| 43 | + { role: 'user', content: 'Say OK again' }, |
| 44 | + ], |
| 45 | + max_tokens: 1000, |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +describe('Mistral provider outbound compatibility filtering', () => { |
| 50 | + it('strips reasoning_content from outgoing requests for api.mistral.ai without mutating the source history', () => { |
| 51 | + const originalRequest = createReasoningRequest(); |
| 52 | + const provider = determineProvider( |
| 53 | + createProviderConfig({ |
| 54 | + baseUrl: 'https://api.mistral.ai/v1', |
| 55 | + model: 'strict-chat-alias', |
| 56 | + }), |
| 57 | + createCliConfig(), |
| 58 | + ); |
| 59 | + |
| 60 | + const result = provider.buildRequest(originalRequest, 'prompt-123'); |
| 61 | + |
| 62 | + expect(result.messages?.[1]).toEqual({ |
| 63 | + role: 'assistant', |
| 64 | + content: 'OK', |
| 65 | + }); |
| 66 | + expect( |
| 67 | + (originalRequest.messages[1] as { reasoning_content?: string }) |
| 68 | + .reasoning_content, |
| 69 | + ).toBe('User asked for a short response.'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('also strips reasoning_content when a Mistral model is served behind a custom base URL', () => { |
| 73 | + const originalRequest = createReasoningRequest(); |
| 74 | + const provider = determineProvider( |
| 75 | + createProviderConfig({ |
| 76 | + baseUrl: 'https://strict-proxy.example.com/v1', |
| 77 | + model: 'Mistral-Large-Latest', |
| 78 | + }), |
| 79 | + createCliConfig(), |
| 80 | + ); |
| 81 | + |
| 82 | + const result = provider.buildRequest(originalRequest, 'prompt-123'); |
| 83 | + |
| 84 | + expect(result.messages?.[1]).toEqual({ |
| 85 | + role: 'assistant', |
| 86 | + content: 'OK', |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('preserves reasoning_content for non-Mistral OpenAI-compatible providers', () => { |
| 91 | + const originalRequest = createReasoningRequest(); |
| 92 | + const provider = determineProvider( |
| 93 | + createProviderConfig({ |
| 94 | + baseUrl: 'https://api.openai.com/v1', |
| 95 | + model: 'gpt-4o', |
| 96 | + }), |
| 97 | + createCliConfig(), |
| 98 | + ); |
| 99 | + |
| 100 | + const result = provider.buildRequest(originalRequest, 'prompt-123'); |
| 101 | + |
| 102 | + expect( |
| 103 | + (result.messages?.[1] as { reasoning_content?: string }) |
| 104 | + .reasoning_content, |
| 105 | + ).toBe('User asked for a short response.'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('does not treat hostile hostnames containing api.mistral.ai as Mistral', () => { |
| 109 | + const originalRequest = createReasoningRequest(); |
| 110 | + const provider = determineProvider( |
| 111 | + createProviderConfig({ |
| 112 | + baseUrl: 'https://api.mistral.ai.evil.example/v1', |
| 113 | + model: 'gpt-4o', |
| 114 | + }), |
| 115 | + createCliConfig(), |
| 116 | + ); |
| 117 | + |
| 118 | + const result = provider.buildRequest(originalRequest, 'prompt-123'); |
| 119 | + |
| 120 | + expect( |
| 121 | + (result.messages?.[1] as { reasoning_content?: string }) |
| 122 | + .reasoning_content, |
| 123 | + ).toBe('User asked for a short response.'); |
| 124 | + }); |
| 125 | +}); |
0 commit comments