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
20 changes: 18 additions & 2 deletions packages/ai/featherless/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ describe('Featherless OpenAI-compatible generation', () => {
temperature: 0.5,
extra: { top_p: 0.9, min_p: 0.05 },
},
{}
{ baseUrl: 'https://api.featherless.test/v1/' }
);

expect(fetchMock).toHaveBeenCalledOnce();
const call = fetchMock.mock.calls[0];
expect(call).toBeDefined();
const [url, request] = call!;
expect(url).toBe('https://api.featherless.ai/v1/chat/completions');
expect(url).toBe('https://api.featherless.test/v1/chat/completions');
expect(request.headers.authorization).toBe('Bearer test-key');
expect(request.headers['HTTP-Referer']).toBe('https://github.com/profullstack/sh1pt');
expect(request.headers['X-Title']).toBe('sh1pt');
Expand All @@ -84,6 +84,22 @@ describe('Featherless OpenAI-compatible generation', () => {
});
});

it.each([
['missing scheme', 'api.featherless.test/v1'],
['ftp scheme', 'ftp://api.featherless.test/v1'],
['credentials', 'https://user:pass@api.featherless.test/v1'],
['query string', 'https://api.featherless.test/v1?token=secret'],
['fragment', 'https://api.featherless.test/v1#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(
/Featherless 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/featherless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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}/chat/completions`, {
const baseUrl = cleanBaseUrl(config.baseUrl ?? DEFAULT_BASE);
const res = await fetch(`${baseUrl}/chat/completions`, {
method: 'POST',
headers: {
authorization: `Bearer ${apiKey}`,
Expand Down Expand Up @@ -82,3 +83,22 @@ interface FeatherlessChatResponse {
completion_tokens?: number;
};
}

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

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

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

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