basics-courses-mcp
Version:
Interactive programming courses from Basics - MCP server for Cursor
331 lines (261 loc) ⢠12.3 kB
JavaScript
// Dynamic instructor wrapper - provides instructions to AI, not hard-coded content
export function createInstructorResponse(rawLessonContent, context) {
// CRITICAL TWO-PHASE INSTRUCTION FOR AI ASSISTANT:
// PHASE 1 - DISPLAY: Show ALL text between markers EXACTLY as written (no summarization)
// PHASE 2 - EXECUTE: After displaying, execute actionable code from the lesson
//
// Actionable (EXECUTE): npm install, git init, create files, update code
// Examples (DON'T EXECUTE): "For example...", "Here's how it works..."
//
// This ensures users see the exact lesson AND get hands-on execution
// Handle different step types
if (context.stepType === 'code') {
return createCodeStepResponse(rawLessonContent, context);
}
else if (context.stepType === 'quiz') {
return createQuizStepResponse(rawLessonContent, context);
}
// Default to content step handling
return createContentStepResponse(rawLessonContent, context);
}
// Helper function to detect if content involves code generation (not just setup)
function detectsCodeGeneration(content) {
const codeGenerationPatterns = [
'let\'s create',
'let\'s build',
'let\'s implement',
'now we\'ll create',
'now we\'ll build',
'now we\'ll implement',
'create a component',
'create a function',
'implement the',
'build a',
'write a function',
'add the following code',
'add this component',
'create the following'
];
const lowerContent = content.toLowerCase();
return codeGenerationPatterns.some(pattern => lowerContent.includes(pattern));
}
function createContentStepResponse(rawLessonContent, context) {
// RULES FOR THIS FUNCTION:
// 1. ALWAYS present rawLessonContent EXACTLY as provided - no changes
// 2. Questions must be about UNDERSTANDING the concepts, not memorization
// 3. Ask questions ONE AT A TIME, not all at once
// 4. Focus on WHY and HOW, not trivial details
// Check if this is welcome/intro content by looking for unique markers
const isWelcome = context.isWelcomeContent ||
rawLessonContent.includes('**Course Details:**') ||
rawLessonContent.includes('**Progress**:') ||
rawLessonContent.includes('**Your Progress Dashboard:**') ||
(rawLessonContent.includes('- Duration:') && rawLessonContent.includes('- Difficulty:')) ||
rawLessonContent.includes('% of course completed');
// Get user's learning mode and question preference
const learningMode = context.userPreferences?.learningMode || 'intermediate';
const questionMode = context.userPreferences?.questionMode || (learningMode === 'beginner' ? 'moderate' : 'none');
const shouldAskQuestions = !isWelcome && questionMode !== 'none';
// Determine number of questions based on preference
let questionCount = '0';
if (questionMode === 'minimal')
questionCount = '1-2';
else if (questionMode === 'moderate')
questionCount = '3-4';
else if (questionMode === 'comprehensive')
questionCount = '5-6';
// Check if content has code generation (for beginner prompt-first approach)
const hasCodeGeneration = detectsCodeGeneration(rawLessonContent);
// Build response based on learning mode
let response = '';
if (learningMode === 'beginner') {
response = createBeginnerModeResponse(rawLessonContent, context, isWelcome, shouldAskQuestions, questionCount, hasCodeGeneration);
}
else if (learningMode === 'intermediate') {
response = createIntermediateModeResponse(rawLessonContent, context, isWelcome, shouldAskQuestions, questionCount);
}
else if (learningMode === 'advanced') {
response = createAdvancedModeResponse(rawLessonContent, context, isWelcome);
}
// Don't add progress or commands - these are internal instructions only
// The AI should handle these internally, not show them to the user
return response;
}
function createBeginnerModeResponse(rawLessonContent, context, isWelcome, shouldAskQuestions, questionCount, hasCodeGeneration) {
// Check if lesson mentions any code files
const hasCodeFiles = rawLessonContent.includes('.js') || rawLessonContent.includes('.ts') ||
rawLessonContent.includes('.jsx') || rawLessonContent.includes('.tsx') ||
rawLessonContent.includes('.py') || rawLessonContent.includes('.java') ||
rawLessonContent.includes('file:') || rawLessonContent.includes('File:');
let response = '';
// For code generation lessons, practice comes FIRST
if (hasCodeGeneration) {
response = `## šÆ Practice: Create with AI
<!-- AI INSTRUCTION: Look at rawLessonContent below and extract what's being built. Present it as a direct challenge based on what they've learned so far -->
Based on what you've learned so far, let's build the next component.
**Your Challenge:**
[EXTRACT from rawLessonContent: What specific thing is being created? E.g., "Create a financial agent that analyzes market data and provides investment recommendations" or "Build a React component that handles user authentication"]
**Context from Previous Lessons:**
[REFERENCE: What have they learned that's relevant? E.g., "You've learned about agents, their core features, the Mastra framework, and how to use tools"]
**Requirements:**
[LIST the specific features/capabilities this should have, extracted from the lesson]
Write a prompt to create this. Be specific about the implementation details you want.
<!-- AI NOTE: After user provides prompt, evaluate honestly, show what's missing, provide ideal prompt, then reveal lesson content -->`;
}
else {
// For non-code-generation lessons, show content first
response = `PHASE 1: Display lesson content EXACTLY as written.
PHASE 2: After displaying, execute any actionable code.
## š LESSON CONTENT:
START_LESSON
${rawLessonContent}
END_LESSON
---
## š§ CODE EXECUTION:
After displaying the content above, analyze and execute actionable items:
- Commands starting with "npm install", "npm init", "git" ā EXECUTE
- File creation with "Create a file...", "Add this to..." ā EXECUTE
- Code blocks after "Let's install...", "Let's create..." ā EXECUTE
- Example code after "For example...", "Here's how..." ā DO NOT EXECUTE
${hasCodeFiles ? `š” **Tip**: Feel free to check out the actual code files mentioned above to understand how things work!
` : ''}
${!hasCodeGeneration && !isWelcome && shouldAskQuestions ? `
## š Understanding Check
<!-- AI INSTRUCTION: Generate ONE understanding question about the lesson content. Ask about WHY something works or HOW it solves problems. Example: "Why do agents need to make autonomous decisions?" -->
Take your time to think about the answer. If you're not sure, it's perfectly fine to say "I don't understand" or ask for clarification!` : ''}
${!hasCodeGeneration && !isWelcome && !shouldAskQuestions ? `
## ā
Ready to Continue
The lesson content has been presented above. Type 'next step' when ready to continue.` : ''}
${isWelcome ? `
## š Ready to Begin
This is the course introduction. When you're ready to start learning, type 'next step'.` : ''}`;
}
return response;
}
function createIntermediateModeResponse(rawLessonContent, context, isWelcome, shouldAskQuestions, questionCount) {
// Check if lesson mentions any code files
const hasCodeFiles = rawLessonContent.includes('.js') || rawLessonContent.includes('.ts') ||
rawLessonContent.includes('.jsx') || rawLessonContent.includes('.tsx') ||
rawLessonContent.includes('.py') || rawLessonContent.includes('.java') ||
rawLessonContent.includes('file:') || rawLessonContent.includes('File:');
let response = `TWO-STEP PROCESS:
1. Display lesson content between markers EXACTLY as written
2. Execute any actionable code from the lesson
## š LESSON CONTENT:
===START===
${rawLessonContent}
===END===
---
## ā” AUTOMATIC EXECUTION:
Now executing actionable items from the lesson:
- Installation commands (npm, pip, etc.) ā AUTO-EXECUTE
- File operations (create, update) ā AUTO-EXECUTE
- Building/running commands ā AUTO-EXECUTE
- Example/demonstration code ā SKIP
${hasCodeFiles ? `š” **Tip**: Check out any code files mentioned to see the implementation!
` : ''}
${!isWelcome ? `
## ā” Next Steps
The lesson content is presented above exactly as written. Any actionable code will be executed automatically.
${shouldAskQuestions ? `
## š Understanding Check
<!-- AI INSTRUCTION: Generate ONE understanding question about WHY or HOW something from the lesson works -->` : ''}
Type 'next step' when ready to continue.` : `
## š Ready to Begin
When you're ready to start, type 'next step'.`}`;
return response;
}
function createAdvancedModeResponse(rawLessonContent, context, isWelcome) {
let response = `DISPLAY THEN EXECUTE:
## š LESSON CONTENT:
<<<EXACT_COPY>>>
${rawLessonContent}
<<<END_EXACT_COPY>>>
---
## ā” Fast Track Execution
Displaying complete lesson above. Now auto-executing ALL actionable code/commands.
Type 'next step' to continue immediately.`;
return response;
}
// Handle beginner mode prompt evaluation
function evaluateUserPrompt(userPrompt, rawLessonContent) {
// Guidelines for AI to evaluate user's prompt
return `
## š Evaluation
**Your prompt:**
"${userPrompt}"
**Feedback:**
[Be honest - would this work? What's good about it? What's missing?]
**Ideal prompt example:**
[Show a prompt that would generate exactly what we need]
---
## š The Actual Implementation:
START_LESSON
${rawLessonContent}
END_LESSON
## š§ Executing the Code:
[Now execute the implementation from the lesson]
`;
}
function createCodeStepResponse(rawLessonContent, context) {
const learningMode = context.userPreferences?.learningMode || 'intermediate';
if (learningMode === 'beginner') {
return `## š„ļø Practice: Implement This
**Challenge:**
${rawLessonContent}
Based on the requirements above, write a prompt to implement this code.
<!-- AI: After user provides prompt, evaluate it, show ideal prompt, then implement the code -->`;
}
else if (learningMode === 'advanced') {
return `## ā” CODE IMPLEMENTATION - ADVANCED MODE
Implementing all code immediately...
${rawLessonContent}
ā
Code implemented. Type 'next step' to continue.`;
}
else {
// Intermediate mode (default)
return `## š„ļø CODE IMPLEMENTATION
**Implementing the following:**
${rawLessonContent}
---
I'll now implement this code and report the results.
Type 'next step' when ready to continue.`;
}
}
function createQuizStepResponse(rawLessonContent, context) {
const learningMode = context.userPreferences?.learningMode || 'intermediate';
if (learningMode === 'beginner') {
return `## š§ ASSESSMENT - BEGINNER MODE
### Your Learning Approach:
Before we check understanding, let's practice describing what you've learned.
**First**: In your own words, explain the main concept from the previous steps.
I'll evaluate your explanation and provide feedback before asking specific questions.
### Topics Covered:
${context.previousSteps?.map(step => `- ${step.title}`).join('\n') || 'Previous lesson content'}
Type your explanation of what you've learned:`;
}
else if (learningMode === 'advanced') {
return `## ā” QUICK ASSESSMENT
Key concepts to verify:
${context.previousSteps?.slice(0, 3).map(step => `- ${step.title}`).join('\n') || 'Previous content'}
Quick check: Can you apply these concepts? (Yes/No)
Type 'next step' to continue.`;
}
else {
// Intermediate
return `## š§ KNOWLEDGE CHECK
Assessing understanding of:
${context.previousSteps?.map(step => `- ${step.title}`).join('\n') || 'Previous lesson content'}
I'll ask a few questions to verify comprehension.
Ready? Type 'next step' when prepared.`;
}
}
// Legacy wrapper for compatibility
export function wrapInInstructorPrompt(content, lessonName, stepName) {
return createInstructorResponse(content, {
lessonName,
stepName,
type: 'step_navigation'
});
}
//# sourceMappingURL=instructor-v2.js.map