diff --git a/src/pages/Chat/Chat.tsx b/src/pages/Chat/Chat.tsx index b74ef59..094f0d1 100644 --- a/src/pages/Chat/Chat.tsx +++ b/src/pages/Chat/Chat.tsx @@ -87,8 +87,52 @@ export default function Chat() { )); } - } catch (error) { + } catch (error: any) { console.error('Failed to send message:', error); + // Default error message + const errorMsg = '连接中断,请重试'; + + setMessages(prev => prev.map(msg => + msg.id === assistantMsgId + ? { ...msg, content: msg.content + `\n\n❌ [${errorMsg}]` } + : msg + )); + } finally { + setIsLoading(false); + } + }; + + // 确认记账 + const handleConfirm = async (card: ConfirmationCard) => { + setIsLoading(true); + try { + await aiService.confirmTransaction(card); + + const confirmMessage: Message = { + id: Date.now().toString(), + role: 'assistant', + content: `✅ 已记录!${card.type === 'income' ? '收入' : '支出'} ${card.amount.toFixed(2)} 元\n分类:${card.category}\n账户:${card.account}`, + timestamp: new Date(), + }; + + setMessages(prev => { + // 移除最后一条消息的确认卡片 + const updated = prev.map((msg, idx) => { + if (idx === prev.length - 1 && msg.confirmationCard) { + return { ...msg, confirmationCard: undefined }; + } + return msg; + }); + return [...updated, confirmMessage]; + }); + + // 清除会话以开始新对话 + aiService.clearSession(); + setSessionId(null); + } catch (error: any) { + console.error('Failed to confirm transaction:', error); + const errorMsg = error.response?.data?.error || '抱歉,记账失败,请稍后再试。'; + const errorMessage: Message = { id: Date.now().toString(), role: 'assistant', @@ -246,7 +290,7 @@ export default function Chat() { ) : ( <> - {messages.map((msg, index) => ( + {messages.map((msg) => (
{msg.role === 'assistant' ? '🪙' : '👤'}