Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/ai/cerebras/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ describe('Cerebras OpenAI-compatible generation', () => {
expect(url).toBe('https://proxy.example.com/v1/chat/completions');
});

it.each([
['missing scheme', 'proxy.example.com'],
['unsupported scheme', 'ftp://proxy.example.com'],
['credentials', 'https://user:pass@proxy.example.com'],
['query string', 'https://proxy.example.com?debug=true'],
['fragment', 'https://proxy.example.com#v1'],
])('rejects unclean configured base URLs: %s', async (_case, baseUrl) => {
const fetchMock = vi.fn();
vi.stubGlobal('fetch', fetchMock);

await expect(adapter.generate(ctx(), 'hello', {}, { baseUrl })).rejects.toThrow(
/Cerebras baseUrl/,
);
expect(fetchMock).not.toHaveBeenCalled();
});

it('includes status and redacted response body excerpt on errors', async () => {
const apiKey = 'test-key-crossing-truncation-boundary';
const prefix = 'x'.repeat(190);
Expand Down
18 changes: 17 additions & 1 deletion packages/ai/cerebras/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,23 @@ interface Config {
const DEFAULT_BASE = 'https://api.cerebras.ai';

function chatCompletionsUrl(baseUrl?: string): string {
return `${(baseUrl ?? DEFAULT_BASE).replace(/\/+$/, '')}/v1/chat/completions`;
return `${cleanBaseUrl(baseUrl ?? DEFAULT_BASE)}/v1/chat/completions`;
}

function cleanBaseUrl(value: string): string {
let url: URL;
try {
url = new URL(value);
} catch {
throw new Error('Cerebras baseUrl must be a valid URL');
}
if (url.protocol !== 'https:' && url.protocol !== 'http:') {
throw new Error('Cerebras baseUrl must use http or https');
}
if (url.username || url.password || url.search || url.hash) {
throw new Error('Cerebras baseUrl must be a clean API base without credentials, query, or hash');
}
return url.toString().replace(/\/+$/, '');
}

function redact(value: string, apiKey: string): string {
Expand Down
Loading