refactor(chatbot): modularize assistant engine and harden moderation …#828
refactor(chatbot): modularize assistant engine and harden moderation …#828alienx5499 merged 6 commits intomainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
QA suite report
Result: passed. QA vs
|
| Base | This PR | Δ passed | |
|---|---|---|---|
| Passed | 659 | 659 | 0 |
| Failed | 0 | 0 | — |
| Total | 659 | 659 | — |
Fixed (failed on base, passing on this PR)
None
New failures (failed on this PR; were not failing on base)
None
Still failing (failed on base and still failing on this PR)
None
There was a problem hiding this comment.
Pull request overview
This PR refactors SortVision’s chatbot into a modular assistant engine, migrates remote chat to /api/chatbot, and adds moderation + multilingual behavior improvements while preserving the chat UX.
Changes:
- Split the monolithic
assistantEngine.jsinto focused modules (constants, intents, templates, resolvers, state, moderation). - Standardized remote chat handling via a new
/api/chatbotroute (OpenAI-compatible NVIDIA upstream + model fallbacks). - Added profanity-based moderation and abuse lockout handling, plus UI-language-aware prompting and localized help/welcome content.
Reviewed changes
Copilot reviewed 19 out of 20 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
SortVision/src/components/chatbot/assistantEngine/templates/localizedHelp.js |
Adds localized help snippets used when UI language isn’t English. |
SortVision/src/components/chatbot/assistantEngine/responseTemplates.js |
Introduces centralized HTML response builders (code, help, algorithm info, etc.). |
SortVision/src/components/chatbot/assistantEngine/resolvers/remoteResponse.js |
Remote response resolver using chatApiClient, with error-specific fallbacks and suggestions. |
SortVision/src/components/chatbot/assistantEngine/resolvers/localResponse.js |
Local resolver for algorithm knowledge + suggestion injection. |
SortVision/src/components/chatbot/assistantEngine/moderation.js |
Adds bad-words profanity detection and a shared abuse threshold. |
SortVision/src/components/chatbot/assistantEngine/intentHandlers.js |
Adds intent scoring, algorithm extraction, and keyword matching helpers. |
SortVision/src/components/chatbot/assistantEngine/core/state.js |
Centralizes message history, conversation context, and response cache. |
SortVision/src/components/chatbot/assistantEngine/contextResponses.js |
Adds contextual/follow-up/reset/fallback response generation. |
SortVision/src/components/chatbot/assistantEngine/constants.js |
Consolidates assistant constants (keywords, patterns, algorithm data, instant responses). |
SortVision/src/components/chatbot/assistantEngine/aiClient.js |
Migrates client to /api/chatbot and injects UI-language steering into prompts. |
SortVision/src/components/chatbot/assistantEngine.js |
New orchestration layer wiring moderation, local/remote routing, caching, and localization. |
SortVision/src/components/chatbot/ChatButton.jsx |
Minor UI tweak (removed robot icon span content). |
SortVision/src/components/chatbot/ChatAssistant.jsx |
Passes selected UI language into assistant context and localizes welcome message. |
SortVision/src/app/api/gemini/route.js |
Removes legacy Gemini route. |
SortVision/src/app/api/chatbot/route.js |
Adds new OpenAI-compatible chat endpoint with abuse tracking and model fallback. |
SortVision/server/index.js |
Updates proxy server to /api/chatbot and adds abuse tracking. |
SortVision/package.json |
Adds bad-words and openai dependencies. |
SortVision/pnpm-lock.yaml |
Locks new dependencies. |
SortVision/.env.example |
Updates environment examples for the new chat provider configuration. |
Files not reviewed (1)
- SortVision/pnpm-lock.yaml: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
b8033a2 to
2f25ca6
Compare
Lighthouse (CI)Lighthouse vs
|
| URL | Perf | Δ | A11y | Δ | Best | Δ | SEO | Δ |
|---|---|---|---|---|---|---|---|---|
| http://localhost:3000/ | 61 | -6 | 96 | — | 93 | — | 100 | — |
| http://localhost:3000/algorithms/config/bubble | 78 | +2 | 96 | — | 93 | — | 100 | — |
| http://localhost:3000/es | 68 | -9 | 96 | — | 93 | — | 100 | — |
| http://localhost:3000/contributions/overview | 67 | — | 96 | — | 96 | — | 100 | — |
Lighthouse (desktop)
Δ = change vs last successful Continuous integration on main (same URLs).
| URL | Perf | Δ | A11y | Δ | Best | Δ | SEO | Δ |
|---|---|---|---|---|---|---|---|---|
| http://localhost:3000/ | 97 | -1 | 96 | — | 96 | — | 100 | — |
| http://localhost:3000/algorithms/config/bubble | 98 | — | 96 | — | 96 | — | 100 | — |
| http://localhost:3000/es | 98 | — | 96 | — | 96 | — | 100 | — |
| http://localhost:3000/contributions/overview | 98 | — | 96 | — | 96 | — | 100 | — |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 19 out of 20 changed files in this pull request and generated 11 comments.
Files not reviewed (1)
- SortVision/pnpm-lock.yaml: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
440cbec to
0fef9de
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 19 out of 20 changed files in this pull request and generated 2 comments.
Files not reviewed (1)
- SortVision/pnpm-lock.yaml: Language not supported
Comments suppressed due to low confidence (1)
SortVision/src/components/chatbot/ChatAssistant.jsx:234
handleErrorcallssetErrorCount(prev => prev + 1)but then decides between “Temporary” vs “Persistent” using the olderrorCountvalue from the closure (errorCount > 2). This makes the threshold off-by-one and can show the wrong message.
Suggestion: compute nextCount inside the setErrorCount updater (or before updating state) and use that value to choose the message, so the UI reflects the incremented count consistently.
const handleError = useCallback(
error => {
console.error('Error: Chat Error:', error);
setErrorCount(prev => prev + 1);
let errorMessage = '';
if (error.message?.includes('TIMEOUT_ERROR')) {
errorMessage = `
<div class="animate-fade-in space-y-1 max-w-full">
<p class="m-0 text-orange-400"> Request Timeout</p>
<p class="m-0 text-sm">The request took too long to process. Let me help you with local knowledge instead!</p>
<p class="m-0 text-xs text-blue-300">Tip: Try asking about specific algorithms!</p>
</div>`;
} else if (error.message?.includes('NETWORK_ERROR')) {
errorMessage = `
<div class="animate-fade-in space-y-1 max-w-full">
<p class="m-0 text-yellow-400"> Connection Issue</p>
<p class="m-0 text-sm">I'm having trouble connecting. Let me help you with local knowledge instead!</p>
<p class="m-0 text-xs text-blue-300">Tip: Try asking about specific algorithms!</p>
</div>`;
} else if (error.message?.includes('RATE_LIMIT')) {
errorMessage = `
<div class="animate-fade-in space-y-1 max-w-full">
<p class="m-0 text-orange-400"> Rate Limit Reached</p>
<p class="m-0 text-sm">I'm getting too many requests. Please wait a moment and try again!</p>
<p class="m-0 text-xs text-blue-300">Tip: In the meantime, try exploring the algorithms above!</p>
</div>`;
} else if (error.message?.includes('SERVER_ERROR')) {
errorMessage = `
<div class="animate-fade-in space-y-1 max-w-full">
<p class="m-0 text-red-400"> Server Issue</p>
<p class="m-0 text-sm">There's a temporary server issue. Let me help you with local knowledge instead!</p>
<p class="m-0 text-xs text-blue-300">Tip: Try asking about specific algorithms!</p>
</div>`;
} else {
errorMessage =
errorCount > 2
? `
<div class="animate-fade-in space-y-1 max-w-full">
<p class="m-0 text-red-400"> Persistent Issue</p>
<p class="m-0 text-sm">I'm having trouble connecting. Please try again later or refresh the page.</p>
<p class="m-0 text-xs text-blue-300">Tip: In the meantime, explore the algorithms above!</p>
</div>`
: `
<div class="animate-fade-in space-y-1 max-w-full">
<p class="m-0 text-yellow-400"> Temporary Issue</p>
<p class="m-0 text-sm">I encountered an error. Let me try to help you again.</p>
<p class="m-0 text-xs text-blue-300">Tip: Try asking about specific algorithms!</p>
</div>`;
}
setMessages(prev => [
...prev,
{
role: 'error',
content: errorMessage,
},
]);
setIsTyping(false);
},
[errorCount]
);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 19 out of 20 changed files in this pull request and generated 3 comments.
Files not reviewed (1)
- SortVision/pnpm-lock.yaml: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This Pull Request refactors SortVision’s chatbot system into smaller focused modules, fully migrates chat API usage to
/api/chatbot, and hardens moderation/language behavior while preserving the overall chat UX.It changes the following:
assistantEngine.jsinto modular units undersrc/components/chatbot/assistantEngine/(constants,intentHandlers,responseTemplates,contextResponses,core/state,resolvers,templates,moderation) so orchestration stays inassistantEngine.js.src/app/api/gemini/route.jsand standardize onsrc/app/api/chatbot/route.jswith robust NVIDIA/OpenAI-compatible request handling and fallback model support.server/index.js,aiClient.js, env examples).Testing
cd SortVision && pnpm run lint && pnpm run test:unit && pnpm run buildcurl:200)403withpolicy: abuse_block)