import React, { useState, useRef, useEffect } from 'react'; import { Send, Loader2, MessageSquare, X } from 'lucide-react'; import { useStore } from '../../store/useStore'; import { generateFlow } from '../../lib/ai'; import { cn } from '../../lib/utils'; const ChatBox: React.FC<{ isMobile?: boolean; onClose?: () => void }> = ({ isMobile, onClose }) => { const [input, setInput] = useState(''); const { messages, addMessage, isLoading, setLoading, nodes, edges, updateFlow } = useStore(); const messagesEndRef = useRef(null); const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }; useEffect(() => { scrollToBottom(); }, [messages]); const handleSend = async (customInput?: string) => { const text = customInput || input; if (!text.trim() || isLoading) return; const userMsg = { id: Date.now().toString(), role: 'user' as const, content: text, createdAt: Date.now(), }; addMessage(userMsg); if (!customInput) setInput(''); setLoading(true); try { const { nodes: newNodes, edges: newEdges, reply } = await generateFlow(text, nodes, edges); updateFlow(newNodes, newEdges); addMessage({ id: (Date.now() + 1).toString(), role: 'assistant', content: reply, createdAt: Date.now(), }); } catch (error: any) { console.error('Chat error:', error); let errorMessage = `抱歉,生成工作流时出现错误:${error.message || '未知错误'}`; if (typeof error.message === 'string' && (error.message.includes('403') || error.message.includes('insufficient'))) { errorMessage = "API 余额不足或权限受限。已为您展示演示流程。"; // 演示模式:如果真失败了,手动模拟一个成功响应,让用户先看到效果 setTimeout(() => { const demoNodes = [ { id: 'd1', type: 'input', position: { x: 250, y: 0 }, data: { label: '收到订单' } }, { id: 'd2', type: 'default', position: { x: 250, y: 150 }, data: { label: '检查库存' } }, { id: 'd3', type: 'default', position: { x: 450, y: 300 }, data: { label: '库存不足' } }, { id: 'd4', type: 'default', position: { x: 50, y: 300 }, data: { label: '扣减库存' } }, { id: 'd5', type: 'output', position: { x: 250, y: 450 }, data: { label: '发货' } }, ]; const demoEdges = [ { id: 'e1-2', source: 'd1', target: 'd2', animated: true }, { id: 'e2-3', source: 'd2', target: 'd3', label: '否' }, { id: 'e2-4', source: 'd2', target: 'd4', label: '是' }, { id: 'e4-5', source: 'd4', target: 'd5', animated: true }, ]; updateFlow(demoNodes, demoEdges); }, 100); } addMessage({ id: (Date.now() + 1).toString(), role: 'assistant', content: errorMessage, createdAt: Date.now(), }); } finally { setLoading(false); } }; const examples = [ "创建一个简单的请假审批流程", "帮我做一个电商下单到发货的流程", "设计一个 AI 图像生成的处理管道", "增加一个'经理二审'的节点在现有流程中" ]; return (

对话工作流助手

Powered by Qwen2.5-7B
{isMobile && ( )}
{messages.length === 0 && (

你好!我是你的工作流助手。
试着告诉我你想创建什么样的流程。

{examples.map((ex, i) => ( ))}
)} {messages.map((msg) => (
{msg.content}
{new Date(msg.createdAt).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}
))}
{isLoading && (
AI 正在思考并生成节点...
)}
setInput(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && handleSend()} disabled={isLoading} />
); }; export default ChatBox;