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
31 changes: 28 additions & 3 deletions packages/ai/wandb/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,51 @@ describe('W&B Inference generation', () => {
});

it('supports text-style choices from compatible responses', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
choices: [{ text: 'legacy text response' }],
}),
}));
});
vi.stubGlobal('fetch', fetchMock);

const result = await adapter.generate(
ctx(),
'hello',
{ model: 'deepseek-ai/DeepSeek-V3-0324' },
{ baseUrl: 'https://wandb.test/v1' },
{ baseUrl: 'https://wandb.test/v1/' },
);

expect(fetchMock.mock.calls[0]?.[0]).toBe('https://wandb.test/v1/chat/completions');
expect(result).toEqual({
text: 'legacy text response',
model: 'deepseek-ai/DeepSeek-V3-0324',
});
});

it('rejects invalid or unclean custom base URLs before network calls', async () => {
const fetchMock = vi.fn();
vi.stubGlobal('fetch', fetchMock);

await expect(
adapter.generate(ctx(), 'hello', {}, { baseUrl: 'wandb.test/v1' }),
).rejects.toThrow('valid URL');
await expect(
adapter.generate(ctx(), 'hello', {}, { baseUrl: 'ftp://wandb.test/v1' }),
).rejects.toThrow('http or https');
await expect(
adapter.generate(ctx(), 'hello', {}, { baseUrl: 'https://user:pass@wandb.test/v1' }),
).rejects.toThrow('clean API base');
await expect(
adapter.generate(ctx(), 'hello', {}, { baseUrl: 'https://wandb.test/v1?target=chat' }),
).rejects.toThrow('clean API base');
await expect(
adapter.generate(ctx(), 'hello', {}, { baseUrl: 'https://wandb.test/v1#chat' }),
).rejects.toThrow('clean API base');

expect(fetchMock).not.toHaveBeenCalled();
});

it('includes status and response body excerpt on errors', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
ok: false,
Expand Down
19 changes: 18 additions & 1 deletion packages/ai/wandb/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export default defineAi<Config>({
};
if (config.project) headers['OpenAI-Project'] = config.project;

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,
body: JSON.stringify({
Expand Down Expand Up @@ -72,6 +73,22 @@ export default defineAi<Config>({
}),
});

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

type WandbRole = 'system' | 'user' | 'assistant' | 'tool';

interface WandbMessage {
Expand Down
Loading