From 1e5016edd8bdc8150c1d7b09a8f53a84ebaff95b Mon Sep 17 00:00:00 2001 From: sahramayo Date: Wed, 10 Sep 2025 08:24:25 -0500 Subject: [PATCH] finished --- .env-example | 2 - middleware/auth.js | 2 +- services/aiService.js | 120 +++++++++++++++++++++++++++--------------- 3 files changed, 80 insertions(+), 44 deletions(-) delete mode 100644 .env-example 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/middleware/auth.js b/middleware/auth.js index 75bc3e7..97ddd80 100644 --- a/middleware/auth.js +++ b/middleware/auth.js @@ -1,7 +1,7 @@ import jwt from "jsonwebtoken"; import prisma from "../lib/prisma.js"; -const JWT_SECRET = process.env.JWT_SECRET || "your-secret-key"; +const JWT_SECRET = process.env.JWT_SECRET || "week21task"; export const authenticateToken = async (req, res, next) => { try { diff --git a/services/aiService.js b/services/aiService.js index 043f837..5e8555e 100644 --- a/services/aiService.js +++ b/services/aiService.js @@ -71,45 +71,83 @@ 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 - // - Use process.env.OPENAI_API_KEY - // - Throw an error if 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 - // - Specify the JSON format it should respond with - // - Include guidelines for creating good tasks and subtasks - // - // 3. Create a user prompt that includes the user's task description - // - Use the 'prompt' parameter passed to this function - // - // 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 - // - // 5. Extract the response content from the API call - // - Access completion.choices[0]?.message?.content - // - Check if response exists, throw error if not - // - // 6. Use the extractTaskFromAIResponse function to parse and validate the response - // - This function is already implemented for you - // - Pass the responseContent to it - // - // 7. Return the parsed task data - // - // 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" - ); + try { + // 1. Check if the OpenAI API key is configured in environment variables + 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 + // - Specify the JSON format it should respond with + // - Include guidelines for creating good tasks and subtasks + const systemPrompt = ` +You are a helpful task management assistant. +Given a user's task description, generate a task in JSON format with optional subtasks. + +Respond ONLY in JSON format like: +{ + "title": "Task title", + "description": "Detailed description", + "status": "pending", // or "in-progress", "completed" + "priority": "medium", // or "low", "high" + "subtasks": [ + { + "title": "Subtask title", + "description": "Subtask description", + "completed": false + } + ] +} + +Guidelines: +- Only return valid JSON (no extra text). +- Title should be clear and specific. +- Subtasks should be concrete and actionable. +- Skip any fields not mentioned above. +`; + + // 3. Create a user prompt that includes the user's task description + // - Use the 'prompt' parameter passed to this function + const userPrompt = `Create a task based on this description: "${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", + temperature: 0.7, + max_tokens: 1000, + messages: [ + { role: "system", content: systemPrompt }, + { role: "user", content: userPrompt }, + ], + }); + + // 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 received from OpenAI"); + } + + // 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; + + } catch (error) { + // 8. Handle errors appropriately + // - Wrap everything in a try-catch block + // - Throw meaningful error messages + throw new Error(`Failed to generate task with AI: ${error.message}`); + } } +