diff --git a/.env-example b/.env-example deleted file mode 100644 index 928533d..0000000 --- a/.env-example +++ /dev/null @@ -1,2 +0,0 @@ -DATABASE_URL=your-database-url -OPENAI_API_KEY=your-openai-api-key \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index a320a24..d9bc5b1 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -26,7 +26,7 @@ model Task { id String @id @default(cuid()) title String description String - status TaskStatus + status TaskStatus @default(TODO) priority Priority dueDate DateTime? assignedTo String? @@ -48,6 +48,7 @@ model Subtask { task Task @relation(fields: [taskId], references: [id], onDelete: Cascade) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt + @@map("subtasks") } diff --git a/services/aiService.js b/services/aiService.js index 043f837..0ea83df 100644 --- a/services/aiService.js +++ b/services/aiService.js @@ -71,45 +71,98 @@ export function extractTaskFromAIResponse(responseContent) { * @returns {Object} Parsed and validated task data */ export async function generateTaskWithAI(prompt) { - // TODO: STUDENT TASK - Complete this function to generate tasks using AI - // - // Instructions: - // 1. Check if the OpenAI API key is configured in environment variables + try { + // TODO: STUDENT TASK - Complete this function to generate tasks using AI + console.log("Generating task with AI..."); + // + // Instructions: + // 1. Check if the OpenAI API key is configured in environment + // variables // - Use process.env.OPENAI_API_KEY // - Throw an error if not configured + if (!process.env.OPENAI_API_KEY) { + throw new Error("OpenAI API key is not configured"); + } + // // 2. Create a system prompt that tells the AI what to do: // - Explain that it's a task management assistant - // - Ask it to create a task with subtasks based on the user's description + // - Ask it to create a task with subtasks based on the user's + // description // - Specify the JSON format it should respond with // - Include guidelines for creating good tasks and subtasks // + const systemPrompt = ` + You are a task management assistant. Your job is to help users create tasks with subtasks. + Please follow these guidelines: + - Create a task with a title and description. + - Include subtasks with titles and descriptions. + - Respond in JSON format. + { + "title": "Task Title", + "description": "Task Description", + "subtasks": [ + { + "title": "Subtask 1 Title", + "description": "Subtask 1 Description" + }, + { + "title": "Subtask 2 Title", + "description": "Subtask 2 Description" + } + ] + } + guidelines: +- The task should be short and clear. +- Subtasks must be specific, actionable, and ordered logically. +- Avoid vague terms like "do research" — instead be precise, e.g., "search online for top 5 tools." +`; + + + // 3. Create a user prompt that includes the user's task description // - Use the 'prompt' parameter passed to this function // + const userPrompt = ` ${prompt} ` // 4. Call the OpenAI API using the openai client: // - Use model: "gpt-3.5-turbo" // - Include both system and user messages // - Set temperature to 0.7 for balanced creativity // - Set max_tokens to 1000 - // + const completion = await openai.chat.completions.create({ + model: "gpt-3.5-turbo", + messages: [ + { role: "system", content: systemPrompt }, + { role: "user", content: userPrompt } + + ], + temperature: 0.7, + max_tokens: 1000 + }); + // 5. Extract the response content from the API call // - Access completion.choices[0]?.message?.content // - Check if response exists, throw error if not - // + const responseContent = completion.choices[0]?.message?.content; + if (!responseContent) { + throw new Error("No response from AI"); + } + + // 6. Use the extractTaskFromAIResponse function to parse and validate the response // - This function is already implemented for you // - Pass the responseContent to it + const taskData = extractTaskFromAIResponse(responseContent); // // 7. Return the parsed task data + return taskData // // 8. Handle errors appropriately // - Wrap everything in a try-catch block // - Throw meaningful error messages // - - throw new Error( - "TODO: Complete the generateTaskWithAI function - see instructions above" - ); + } catch (error) { + throw new Error(`Failed to generate task with AI: ${error.message}`); +} }