feat: 新增聊天页面,支持文本和语音输入、AI回复流式展示及记账确认功能。

This commit is contained in:
2026-01-30 13:09:40 +08:00
parent 9adf293489
commit 48ce12532c

View File

@@ -87,8 +87,52 @@ export default function Chat() {
)); ));
} }
} catch (error) { } catch (error: any) {
console.error('Failed to send message:', error); 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 = { const errorMessage: Message = {
id: Date.now().toString(), id: Date.now().toString(),
role: 'assistant', role: 'assistant',
@@ -246,7 +290,7 @@ export default function Chat() {
</div> </div>
) : ( ) : (
<> <>
{messages.map((msg, index) => ( {messages.map((msg) => (
<div key={msg.id} className={`chat-message ${msg.role}`}> <div key={msg.id} className={`chat-message ${msg.role}`}>
<div className="chat-message-avatar"> <div className="chat-message-avatar">
{msg.role === 'assistant' ? '🪙' : '👤'} {msg.role === 'assistant' ? '🪙' : '👤'}