fix: wrap AI block in try/catch for graceful fallback when Shakespeare API fails

If the AI call throws (400, 401, network error, etc.), Sasquatch now:
- Catches the error silently (logs to console as warning)
- Falls back to the static FAQ answer if one matches
- Falls back to a friendly "try again" message if no FAQ match
- Never shows a crash or blank response to the user

The toolMatch reference inside the try block is properly scoped so the
isLive flag works correctly when the full AI + CVM flow succeeds.

Co-authored-by: shakespeare.diy <assistant@shakespeare.diy>
This commit is contained in:
shakespeare.diy 2026-06-16 19:47:49 +07:00
parent 054c06d573
commit 632de3e5bd

View file

@ -184,9 +184,11 @@ export function SasquatchGuide({ message, onDismiss }: SasquatchGuideProps) {
}));
history.push({ role: 'user', content: question });
// 3. If user is logged in, use Shakespeare AI with streaming
// 3. If user is logged in, try Shakespeare AI (with graceful fallback)
if (user && shakespeare.isAuthenticated) {
let accumulated = '';
let aiSucceeded = false;
try {
// First pass: get AI response which may include a TOOL: call
// Prefix the system prompt into the first user message for compatibility
@ -274,6 +276,17 @@ export function SasquatchGuide({ message, onDismiss }: SasquatchGuideProps) {
timestamp: Date.now(),
isLive: !!toolMatch && cvmConnected === true,
});
aiSucceeded = true;
} catch (aiErr) {
// AI failed — fall back to static FAQ silently
console.warn('[Sasquatch] AI unavailable, using static FAQ:', aiErr);
}
if (!aiSucceeded) {
const fallback = staticAnswer ??
"Couldn't reach the AI right now. Check the map for spots, The Lab for coaches, or ask again in a moment! 🛹";
appendMessage({ role: 'sasquatch', text: fallback, timestamp: Date.now() });
}
} else {
// 6. Not logged in — use static FAQ or generic fallback