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
25 changes: 22 additions & 3 deletions packages/ai/nebius/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,41 @@ describe('Nebius Token Factory chat completions generation', () => {
});

it('supports text-style choices from compatible Nebius responses', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
model: 'meta-llama/Llama-3.3-70B-Instruct',
choices: [{ text: 'text choice response' }],
}),
}));
});
vi.stubGlobal('fetch', fetchMock);

const result = await adapter.generate(ctx(), 'hello', {}, { baseUrl: 'https://nebius.test' });
const result = await adapter.generate(ctx(), 'hello', {}, { baseUrl: 'https://nebius.test/' });

expect(fetchMock).toHaveBeenCalledOnce();
expect(fetchMock.mock.calls[0]?.[0]).toBe('https://nebius.test/v1/chat/completions');
expect(result).toEqual({
text: 'text choice response',
model: 'meta-llama/Llama-3.3-70B-Instruct',
});
});

it.each([
['missing scheme', 'nebius.test'],
['ftp scheme', 'ftp://nebius.test'],
['credentials', 'https://user:pass@nebius.test'],
['query string', 'https://nebius.test?token=secret'],
['fragment', 'https://nebius.test#chat'],
])('rejects unclean custom baseUrl values: %s', async (_label, baseUrl) => {
const fetchMock = vi.fn();
vi.stubGlobal('fetch', fetchMock);

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

it('includes status and response body excerpt on errors', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
ok: false,
Expand Down
22 changes: 21 additions & 1 deletion packages/ai/nebius/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export default defineAi<Config>({
if (opts.system) messages.push({ role: 'system', content: opts.system });
messages.push({ role: 'user', content: prompt });

const res = await fetch(`${config.baseUrl ?? DEFAULT_BASE}/v1/chat/completions`, {
const baseUrl = cleanBaseUrl(config.baseUrl ?? DEFAULT_BASE);
const res = await fetch(`${baseUrl}/v1/chat/completions`, {
method: 'POST',
headers: {
authorization: `Bearer ${apiKey}`,
Expand Down Expand Up @@ -87,3 +88,22 @@ interface NebiusChatResponse {
completion_tokens?: number;
};
}

function cleanBaseUrl(value: string): string {
let url: URL;
try {
url = new URL(value);
} catch {
throw new Error('Nebius baseUrl must be a valid URL');
}

if (url.protocol !== 'https:' && url.protocol !== 'http:') {
throw new Error('Nebius baseUrl must use http or https');
}

if (url.username || url.password || url.search || url.hash) {
throw new Error('Nebius baseUrl must be a clean API base without credentials, query, or hash');
}

return url.toString().replace(/\/+$/, '');
}
Loading