import { GoogleGenAI, Type } from "@google/genai"; const BOOK_DATA_SCHEMA = { type: Type.OBJECT, properties: { title: { type: Type.STRING, description: 'Title of the book' }, author: { type: Type.STRING, description: 'Author of the book' }, genre: { type: Type.STRING, description: 'Main genre of the book' }, annotation: { type: Type.STRING, description: 'Brief summary or annotation' }, pages: { type: Type.NUMBER, description: 'Approximate number of pages' }, language: { type: Type.STRING, description: 'Language of the book' }, publishedYear: { type: Type.NUMBER, description: 'Approximate publication year' } }, required: ['title', 'author', 'genre'] }; export async function extractBookData(base64Image: string) { // Ensure we use the exact initialization pattern from guidelines const ai = new GoogleGenAI({ apiKey: process.env.API_KEY }); try { // Use gemini-3-flash-preview for multimodal extraction tasks const response = await ai.models.generateContent({ model: 'gemini-3-flash-preview', contents: { parts: [ { inlineData: { mimeType: 'image/jpeg', data: base64Image.replace(/^data:image\/[a-z]+;base64,/, "") } }, { text: "Identify this book from the cover. Extract the title, author, genre, and a brief annotation in Russian. Return the information in structured JSON format." } ] }, config: { responseMimeType: "application/json", responseSchema: BOOK_DATA_SCHEMA } }); const text = response.text; if (!text) throw new Error("No response from model"); return JSON.parse(text); } catch (error: any) { console.error("Failed to extract book data:", error); // Log specific details if available if (error.message) { console.error("Error Message:", error.message); } throw error; } }