feat(gemini): fetch URL images as inlineData for multimodal prompts

This commit is contained in:
William Valentin
2026-02-16 23:50:51 -08:00
parent 63adec9cea
commit bdded84a9b
+41
View File
@@ -164,6 +164,47 @@ describe('GeminiClient', () => {
expect(response.stopReason).toBe('max_tokens');
});
it('fetches URL image content and sends inlineData parts', async () => {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
headers: { get: (name: string) => name.toLowerCase() === 'content-type' ? 'image/png' : null },
arrayBuffer: async () => Uint8Array.from([1, 2, 3, 4]).buffer,
} as unknown as Response);
vi.stubGlobal('fetch', fetchMock);
const client = new GeminiClient({
apiKey: 'test-key',
model: 'gemini-2.0-flash',
});
await client.chat({
messages: [{
role: 'user',
content: [
{ type: 'text', text: 'describe this image' },
{ type: 'image', source: { type: 'url', url: 'https://example.com/test.png', media_type: 'image/png' } },
],
}],
});
expect(fetchMock).toHaveBeenCalledWith('https://example.com/test.png');
expect(mockGenerateContent).toHaveBeenCalledWith({
contents: [{
role: 'user',
parts: [
{ text: 'describe this image' },
{
inlineData: {
mimeType: 'image/png',
data: Buffer.from([1, 2, 3, 4]).toString('base64'),
},
},
],
}],
});
vi.unstubAllGlobals();
});
it('uses environment variable for API key when not provided', () => {
const originalEnv = process.env.GOOGLE_API_KEY;
process.env.GOOGLE_API_KEY = 'env-key';