SmartAssistant.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import React, { useRef, useEffect, useState } from 'react';
  2. import { FloatingButton } from './FloatingButton';
  3. import { ChatWindow } from './ChatWindow';
  4. import { AgentSelector } from './AgentSelector';
  5. import { useAIAgent } from './hooks/useAIAgent';
  6. import type { SmartAssistantProps } from './types';
  7. import { INK_THEME } from './styles/ink-theme';
  8. export const SmartAssistant: React.FC<SmartAssistantProps> = ({
  9. isOpen,
  10. onToggle,
  11. defaultAgentId,
  12. mode = 'floating',
  13. containerId
  14. }) => {
  15. const messagesEndRef = useRef<HTMLDivElement>(null);
  16. const [inputText, setInputText] = useState('');
  17. const {
  18. agents,
  19. selectedAgent,
  20. messages,
  21. isLoading,
  22. isAgentsLoading,
  23. error,
  24. selectAgent,
  25. sendMessage,
  26. clearMessages,
  27. } = useAIAgent(defaultAgentId);
  28. // 自动滚动到底部
  29. const scrollToBottom = () => {
  30. messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
  31. };
  32. // 监听消息变化,自动滚动
  33. useEffect(() => {
  34. if (messages.length > 0) {
  35. scrollToBottom();
  36. }
  37. }, [messages]);
  38. // 处理发送消息
  39. const handleSend = async (text: string) => {
  40. await sendMessage(text);
  41. };
  42. // 处理切换智能体
  43. const handleAgentChange = (agent: any) => {
  44. if (agent && agent.id) {
  45. selectAgent(agent);
  46. }
  47. };
  48. // 处理关闭
  49. const handleClose = () => {
  50. onToggle();
  51. };
  52. // 处理输入变化
  53. const handleInputChange = (text: string) => {
  54. setInputText(text);
  55. };
  56. if (mode === 'embedded' && containerId) {
  57. return (
  58. <div className="w-full h-full flex flex-col" style={{ backgroundColor: INK_THEME.colors.ink.light }}>
  59. <AgentSelector
  60. agents={agents}
  61. selectedAgent={selectedAgent}
  62. onAgentChange={handleAgentChange}
  63. />
  64. <ChatWindow
  65. isOpen={true}
  66. messages={messages}
  67. inputText={inputText}
  68. onSend={handleSend}
  69. onClose={handleClose}
  70. onInputChange={handleInputChange}
  71. messagesEndRef={messagesEndRef}
  72. agents={agents}
  73. selectedAgent={selectedAgent}
  74. onAgentChange={handleAgentChange}
  75. isLoading={isLoading}
  76. isAgentsLoading={isAgentsLoading}
  77. />
  78. </div>
  79. );
  80. }
  81. return (
  82. <>
  83. <FloatingButton
  84. isOpen={isOpen}
  85. onClick={onToggle}
  86. unreadCount={0}
  87. />
  88. <ChatWindow
  89. isOpen={isOpen}
  90. messages={messages}
  91. inputText={inputText}
  92. onSend={handleSend}
  93. onClose={handleClose}
  94. onInputChange={handleInputChange}
  95. messagesEndRef={messagesEndRef}
  96. agents={agents}
  97. selectedAgent={selectedAgent}
  98. onAgentChange={handleAgentChange}
  99. isLoading={isLoading}
  100. isAgentsLoading={isAgentsLoading}
  101. />
  102. </>
  103. );
  104. };
  105. export default SmartAssistant;