define actions in errorPatterns

This commit is contained in:
sam committed 2025-09-22 20:54:43 +07:00
1 parent ff95c13f84
commit 0e80915d58
2 files changed
+19 -24

No files matched your search

+2 -16
View File
@@ -299,7 +299,7 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
// Function to suggest error fix
// Clean alert hooks with proper interfaces
const aiModelAlert = useAIModelErrorAlert(messages);
const aiModelAlert = useAIModelErrorAlert(messages, _onNewChat, openModelSelector);
const consoleAlert = useConsoleErrorAlert();
// Scroll to bottom when alerts appear
@@ -316,20 +316,6 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
setInput(`The user gets ${errorText} in the app now: ${errorMessage}\n\nCan you identify the problem and create a concise fix.`);
}, []);
const getAIModelAction = useCallback((errorType: 'warn' | 'error' | null) => {
if (errorType === 'error') {
return {
label: 'Change model',
onClick: openModelSelector
};
} else if (errorType === 'warn') {
return {
label: 'New chat',
onClick: _onNewChat
};
}
return undefined;
}, [openModelSelector, _onNewChat]);
// Check for autostart parameter and trigger AI generation
useEffect(() => {
@@ -682,7 +668,7 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
type={aiModelAlert.errorType || 'error'}
message={aiModelAlert.errorMessage || ''}
onDismiss={aiModelAlert.dismiss}
action={getAIModelAction(aiModelAlert.errorType)}
action={aiModelAlert.action}
/>
)}
+17 -8
View File
@@ -47,31 +47,38 @@ export interface AIModelErrorAlert {
hasError: boolean;
errorMessage: string | null;
errorType: 'warn' | 'error' | null;
action?: { label: string; onClick: () => void };
dismiss: () => void;
}
export const useAIModelErrorAlert = (messages: any[]): AIModelErrorAlert => {
export const useAIModelErrorAlert = (
messages: any[],
onNewChat?: () => void,
onChangeModel?: () => void
): AIModelErrorAlert => {
const [dismissedAlertKey, setDismissedAlertKey] = useState<string>('');
const { hasError, errorMessage, errorType } = useMemo(() => {
const { hasError, errorMessage, errorType, action } = useMemo(() => {
const lastMessage = messages[messages.length - 1];
if (!lastMessage || lastMessage.role !== 'assistant') {
return { hasError: false, errorMessage: null, errorType: null };
return { hasError: false, errorMessage: null, errorType: null, action: undefined };
}
const content = typeof lastMessage.content === 'string' ? lastMessage.content : '';
// Define error patterns
// Define error patterns with actions
const errorPatterns = [
{
test: (content: string) => /AI service error: 422/.test(content) || /Provider returned error/.test(content),
type: 'error' as const,
message: 'The AI model encountered an issue. Try switching to a different model or starting a new chat.'
message: 'The AI model encountered an issue. Try switching to a different model or starting a new chat.',
action: onChangeModel ? { label: 'Change model', onClick: onChangeModel } : undefined
},
{
test: (content: string) => /maximum context length is \d+ tokens/.test(content) || /context length/.test(content),
type: 'warn' as const,
message: 'Your conversation is too long for this model. Try starting a new chat or switching to a model with a larger context window.'
message: 'Your conversation is too long for this model. Try starting a new chat or switching to a model with a larger context window.',
action: onNewChat ? { label: 'New chat', onClick: onNewChat } : undefined
},
{
test: (content: string) => /rate limit/i.test(content) || /too many requests/i.test(content),
@@ -88,13 +95,14 @@ export const useAIModelErrorAlert = (messages: any[]): AIModelErrorAlert => {
hasError: dismissedAlertKey !== alertKey,
errorMessage: pattern.message,
errorType: pattern.type,
action: pattern.action,
alertKey
};
}
}
return { hasError: false, errorMessage: null, errorType: null };
}, [messages, dismissedAlertKey]);
return { hasError: false, errorMessage: null, errorType: null, action: undefined };
}, [messages, dismissedAlertKey, onNewChat, onChangeModel]);
const dismiss = useCallback(() => {
const lastMessage = messages[messages.length - 1];
@@ -108,6 +116,7 @@ export const useAIModelErrorAlert = (messages: any[]): AIModelErrorAlert => {
hasError,
errorMessage,
errorType,
action,
dismiss
};
};