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:
parent
054c06d573
commit
632de3e5bd
1 changed files with 21 additions and 8 deletions
|
|
@ -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
|
||||
|
|
@ -267,13 +269,24 @@ export function SasquatchGuide({ message, onDismiss }: SasquatchGuideProps) {
|
|||
accumulated = aiText;
|
||||
}
|
||||
|
||||
setStreamingText('');
|
||||
appendMessage({
|
||||
role: 'sasquatch',
|
||||
text: accumulated || "Hmm, couldn't process that. Try rephrasing!",
|
||||
timestamp: Date.now(),
|
||||
isLive: !!toolMatch && cvmConnected === true,
|
||||
});
|
||||
setStreamingText('');
|
||||
appendMessage({
|
||||
role: 'sasquatch',
|
||||
text: accumulated || "Hmm, couldn't process that. Try rephrasing!",
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue