Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .env-example

This file was deleted.

131 changes: 90 additions & 41 deletions services/aiService.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,45 +71,94 @@ 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
if (!process.env.OPENAI_API_KEY) {
throw new Error("OpenAI API key not configured in environment variables");
}

// 2. Create a system prompt that tells the AI what to do
const systemPrompt = `You are a task management assistant that helps users break down their goals into actionable tasks and subtasks.

Your job is to:
- Create a clear, actionable main task based on the user's description
- Break it down into 3-5 specific subtasks that help complete the main task
- Assign appropriate priority and status
- Provide helpful descriptions

Guidelines:
- Make tasks specific and actionable
- Each subtask should be a concrete step toward completing the main task
- Use clear, professional language
- Set realistic priorities (low, medium, high, urgent)
- Default status should be "pending"

IMPORTANT: Respond ONLY with valid JSON in exactly this format:
{
"title": "Main task title",
"description": "Detailed description of what needs to be accomplished",
"priority": "medium",
"status": "pending",
"subtasks": [
{
"title": "Subtask 1 title",
"description": "Description of what this subtask involves",
"completed": false
},
{
"title": "Subtask 2 title",
"description": "Description of what this subtask involves",
"completed": false
}
]
}`;

// 3. Create a user prompt that includes the user's task description
const userPrompt = `Please create a task with subtasks for: ${prompt}`;

// 4. Call the OpenAI API
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
const responseContent = completion.choices[0]?.message?.content;

if (!responseContent) {
throw new Error("No response received from OpenAI API");
}

// 6. Use the extractTaskFromAIResponse function to parse and validate the response
const parsedTaskData = extractTaskFromAIResponse(responseContent);

// 7. Return the parsed task data
return parsedTaskData;

} catch (error) {
// 8. Handle errors appropriately
if (error.message.includes("OpenAI API key not configured")) {
throw error; // Re-throw configuration errors as-is
}

if (error.code === 'insufficient_quota') {
throw new Error("OpenAI API quota exceeded. Please check your billing settings.");
}

if (error.code === 'invalid_api_key') {
throw new Error("Invalid OpenAI API key. Please check your configuration.");
}

if (error.message.includes("Failed to extract task from AI response")) {
throw new Error(`Failed to parse AI response: ${error.message}`);
}

// For any other errors, provide a meaningful message
throw new Error(`AI task generation failed: ${error.message}`);
}
}