Skip to content
Merged
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
1 change: 1 addition & 0 deletions BackendAcademy/src/ai/ai.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const aiProviderFactory = {

@Module({
controllers: [AiController],
//ai controller
providers: [AiService, PromptTemplateService, aiProviderFactory],
exports: [AiService, PromptTemplateService],
})
Expand Down
106 changes: 106 additions & 0 deletions BackendAcademy/src/ai/ai.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,59 @@ export class AiService {
};
}


async getRecommendation(userId: string): Promise<AiRecommendationResponse> {
const snapshot = this.redisService
? await this.redisService.getUserSnapshot(userId)
: null;

if (!snapshot) {
return {
userId,
recommendations: [],
explainability: {
factors: ['insufficient_data'],
confidence: 0.1,
userSignalAge: 0,
signalsUsed: [],
modelVersion: 'rustacademy-recommender-v2',
},
generatedAt: new Date(),
};
}

const explainability = this.redisService
? await this.redisService.getRecommendationExplainability(userId)
: null;

const recommendedCourses = snapshot.recentCourses.length > 0
? snapshot.recentCourses.slice(0, 3)
: ['rust-fundamentals', 'smart-contracts-101', 'stellar-basics'];

const recommendations = recommendedCourses.map((courseId, index) => ({
courseId,
score: Math.max(0, 1 - index * 0.2 - (snapshot.interactionCount > 0 ? 0 : 0.3)),
reason: explainability?.factors[index] || 'course_popularity',
}));

if (this.monitoringService) {
this.monitoringService.recordDomainEvent('recommendation_generated', 'ai');
}

return {
userId,
recommendations,
explainability: explainability || {
factors: [],
confidence: 0.1,
userSignalAge: 0,
signalsUsed: [],
modelVersion: 'rustacademy-recommender-v2',
},
generatedAt: new Date(),
};
}

/**
* Calls the AI provider with the global request timeout (Issue #408) and
* falls back to a static response if the provider is unavailable, times
Expand Down Expand Up @@ -297,6 +350,59 @@ export class AiService {
);
}


async getRecommendation(userId: string): Promise<AiRecommendationResponse> {
const snapshot = this.redisService
? await this.redisService.getUserSnapshot(userId)
: null;

if (!snapshot) {
return {
userId,
recommendations: [],
explainability: {
factors: ['insufficient_data'],
confidence: 0.1,
userSignalAge: 0,
signalsUsed: [],
modelVersion: 'rustacademy-recommender-v2',
},
generatedAt: new Date(),
};
}

const explainability = this.redisService
? await this.redisService.getRecommendationExplainability(userId)
: null;

const recommendedCourses = snapshot.recentCourses.length > 0
? snapshot.recentCourses.slice(0, 3)
: ['rust-fundamentals', 'smart-contracts-101', 'stellar-basics'];

const recommendations = recommendedCourses.map((courseId, index) => ({
courseId,
score: Math.max(0, 1 - index * 0.2 - (snapshot.interactionCount > 0 ? 0 : 0.3)),
reason: explainability?.factors[index] || 'course_popularity',
}));

if (this.monitoringService) {
this.monitoringService.recordDomainEvent('recommendation_generated', 'ai');
}

return {
userId,
recommendations,
explainability: explainability || {
factors: [],
confidence: 0.1,
userSignalAge: 0,
signalsUsed: [],
modelVersion: 'rustacademy-recommender-v2',
},
generatedAt: new Date(),
};
}

const lines = code.split('\n').filter((l) => l.trim().length > 0).length;
const hasComments = code.includes('//') || code.includes('/*');
const hasFunctions = code.includes('fn ');
Expand Down
Loading