|
1 | | -import { books } from "../data/books"; |
2 | | -import { lessons } from "../data/lessons"; |
3 | | - |
4 | | -/** |
5 | | - * Simulates extracting lessons from a book. |
6 | | - * Returns a Promise that resolves after a 1-second delay (simulating AI processing). |
7 | | - * Later, this can be swapped for a real AI API call. |
8 | | - */ |
9 | | -export function extractLessons(bookTitle) { |
10 | | - return new Promise((resolve, reject) => { |
11 | | - setTimeout(() => { |
12 | | - const book = books.find( |
13 | | - (b) => b.title.toLowerCase() === bookTitle.toLowerCase(), |
14 | | - ); |
15 | | - |
16 | | - if (!book) { |
17 | | - reject(new Error(`Book "${bookTitle}" not found in database.`)); |
18 | | - return; |
19 | | - } |
20 | | - |
21 | | - const bookLessons = lessons |
22 | | - .filter((l) => l.bookId === book.id) |
23 | | - .sort((a, b) => a.dayOfWeek - b.dayOfWeek); |
24 | | - |
25 | | - resolve({ book, lessons: bookLessons }); |
26 | | - }, 1000); |
| 1 | +const OPENAI_API_URL = "https://api.openai.com/v1/chat/completions"; |
| 2 | + |
| 3 | +export async function extractLessons(book) { |
| 4 | + const apiKey = import.meta.env.VITE_OPENAI_API_KEY; |
| 5 | + |
| 6 | + if (!apiKey) { |
| 7 | + throw new Error( |
| 8 | + "OpenAI API key is not configured. Add VITE_OPENAI_API_KEY to your .env file.", |
| 9 | + ); |
| 10 | + } |
| 11 | + |
| 12 | + const prompt = `You are a book lesson extractor. Given the book "${book.title}" by ${book.author}, generate exactly 7 practical daily lessons a reader can apply over one week. |
| 13 | +
|
| 14 | +Return a JSON array of 7 objects with these fields: |
| 15 | +- id (number, 1-7) |
| 16 | +- title (string, short lesson title) |
| 17 | +- description (string, 2-3 sentences explaining the lesson) |
| 18 | +- actionStep (string, one concrete action the reader can take today) |
| 19 | +- category (string, a short category label like "Habits", "Focus", "Mindset", etc.) |
| 20 | +- dayOfWeek (number, 1-7 for Monday-Sunday) |
| 21 | +
|
| 22 | +Return ONLY the JSON array, no markdown fences or extra text.`; |
| 23 | + |
| 24 | + const res = await fetch(OPENAI_API_URL, { |
| 25 | + method: "POST", |
| 26 | + headers: { |
| 27 | + "Content-Type": "application/json", |
| 28 | + Authorization: `Bearer ${apiKey}`, |
| 29 | + }, |
| 30 | + body: JSON.stringify({ |
| 31 | + model: "gpt-4o-mini", |
| 32 | + messages: [{ role: "user", content: prompt }], |
| 33 | + temperature: 0.7, |
| 34 | + max_tokens: 2000, |
| 35 | + }), |
27 | 36 | }); |
| 37 | + |
| 38 | + if (!res.ok) { |
| 39 | + const errorData = await res.json().catch(() => ({})); |
| 40 | + throw new Error( |
| 41 | + errorData.error?.message || `OpenAI API error: ${res.status}`, |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + const data = await res.json(); |
| 46 | + const content = data.choices[0].message.content.trim(); |
| 47 | + |
| 48 | + let lessons; |
| 49 | + try { |
| 50 | + lessons = JSON.parse(content); |
| 51 | + } catch { |
| 52 | + const match = content.match(/\[[\s\S]*\]/); |
| 53 | + if (match) { |
| 54 | + lessons = JSON.parse(match[0]); |
| 55 | + } else { |
| 56 | + throw new Error("Failed to parse lessons from AI response."); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return { book, lessons }; |
28 | 61 | } |
0 commit comments