feat(gemini): fetch URL images as inlineData for multimodal prompts
This commit is contained in:
@@ -164,6 +164,47 @@ describe('GeminiClient', () => {
|
|||||||
expect(response.stopReason).toBe('max_tokens');
|
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', () => {
|
it('uses environment variable for API key when not provided', () => {
|
||||||
const originalEnv = process.env.GOOGLE_API_KEY;
|
const originalEnv = process.env.GOOGLE_API_KEY;
|
||||||
process.env.GOOGLE_API_KEY = 'env-key';
|
process.env.GOOGLE_API_KEY = 'env-key';
|
||||||
|
|||||||
Reference in New Issue
Block a user