| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>WMS智能助手</title>
- </head>
- <body>
- <div class="warehouse-chat">
- <!-- 侧边栏 -->
- <div class="sidebar">
- <div class="logo">
- <div class="logo-icon">
- <span style="font-size: 20px;">🤖</span>
- </div>
- </div>
- <div class="nav-buttons">
- <button class="nav-btn active">
- <span class="nav-icon">💬</span>
- </button>
- <button class="nav-btn">
- <span class="nav-icon">📊</span>
- </button>
- <button class="nav-btn">
- <span class="nav-icon">🔄</span>
- </button>
- </div>
- </div>
- <!-- 主聊天区域 -->
- <div class="chat-container">
- <!-- 欢迎界面 -->
- <div id="welcome-screen" class="welcome-screen">
- <div class="welcome-content">
- <div class="welcome-icon">
- <span style="font-size: 64px;">🤖</span>
- </div>
- <h1 class="welcome-title">我是WMS智能助手,很高兴见到你!</h1>
- <p class="welcome-subtitle">我可以帮您处理订单加急标记、创建入库预约等任务,请把你的任务交给我~</p>
- <!-- 推荐功能 -->
- <div class="recommendations">
- <div class="recommendation-section">
- <h3>📦 订单加急</h3>
- <p>例如:"订单SOZ25090400035加急处理"</p>
- </div>
- <div class="recommendation-section">
- <h3>🚚 创建入库预约</h3>
- <p>例如:"梁涵9月20日上午送100箱货到九干仓,单号T2025072101115676"</p>
- </div>
- <div class="examples">
- <div class="example" onclick="sendExample('订单SOZ25090400035加急处理')">订单SOZ25090400035加急处理</div>
- <div class="example" onclick="sendExample('梁涵9月20日上午送100箱货到九干仓,单号T2025072101115676')">梁涵9月20日上午送100箱货到九干仓,单号T2025072101115676</div>
- <div class="example" onclick="sendExample('傲竹,订单BSIM2510100010004预计10月13日上午到货')">傲竹,订单BSIM2510100010004预计10月13日上午到货</div>
- </div>
- </div>
- </div>
- </div>
- <!-- 聊天消息 -->
- <div id="messages-container" class="messages-container" style="display: none;">
- <div id="messages"></div>
- <!-- 加载状态 -->
- <div id="loading-message" class="message assistant" style="display: none;">
- <div class="message-content">
- <div class="assistant-message">
- <div class="assistant-icon">
- <span style="font-size: 16px;">🤖</span>
- </div>
- <div class="typing-indicator">
- <span class="dot"></span>
- <span class="dot"></span>
- <span class="dot"></span>
- </div>
- </div>
- </div>
- </div>
- </div>
- <!-- 输入区域 -->
- <div class="input-container">
- <div class="input-box">
- <div class="input-wrapper">
- <input
- id="message-input"
- type="text"
- placeholder="给WMS智能助手发送消息"
- class="message-input"
- onkeypress="handleKeyPress(event)"
- />
- <div class="input-actions">
- <button class="action-btn" title="深度思考">
- 🧠
- </button>
- <button class="action-btn" title="联网搜索">
- 🌐
- </button>
- <button
- id="send-btn"
- class="send-btn"
- onclick="sendMessage()"
- >
- ↑
- </button>
- </div>
- </div>
- </div>
- <p class="disclaimer">内容由 AI 生成,请仔细核验</p>
- </div>
- </div>
- </div>
- <script>
- // 全局变量
- const API_BASE_URL = 'http://127.0.0.1:8080';
- let messages = [];
- let isLoading = false;
- // DOM元素
- let welcomeScreen;
- let messagesContainer;
- let messagesDiv;
- let messageInput;
- let sendBtn;
- let loadingMessage;
- let currentAssistantElement = null;
- let currentAssistantText = '';
- // 初始化
- document.addEventListener('DOMContentLoaded', function() {
- welcomeScreen = document.getElementById('welcome-screen');
- messagesContainer = document.getElementById('messages-container');
- messagesDiv = document.getElementById('messages');
- messageInput = document.getElementById('message-input');
- sendBtn = document.getElementById('send-btn');
- loadingMessage = document.getElementById('loading-message');
- // 自动聚焦输入框
- messageInput.focus();
- console.log('聊天界面已初始化');
- });
- // 处理键盘事件
- function handleKeyPress(event) {
- if (event.key === 'Enter' && !event.shiftKey) {
- event.preventDefault();
- sendMessage();
- }
- }
- // 发送示例消息
- function sendExample(text) {
- messageInput.value = text;
- sendMessage();
- }
- // 发送消息
- async function sendMessage() {
- const message = messageInput.value.trim();
- if (!message || isLoading) return;
- // 隐藏欢迎界面,显示消息容器
- if (messages.length === 0) {
- welcomeScreen.style.display = 'none';
- messagesContainer.style.display = 'block';
- }
- // 禁用输入和按钮
- setInputState(false);
- // 添加用户消息
- addUserMessage(message);
- // 显示加载状态
- showLoading();
- // 清空输入框
- messageInput.value = '';
- try {
- const response = await fetch(`${API_BASE_URL}/api/agent/chat/stream`, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify({
- message: message
- })
- });
- await handleStreamResponse(response);
- } catch (error) {
- console.error('请求失败:', error);
- // 隐藏加载状态
- hideLoading();
- // 添加错误消息
- addBotMessage('❌ 网络错误,请稍后重试。');
- } finally {
- // 重新启用输入和按钮
- setInputState(true);
- messageInput.focus();
- }
- }
- // 添加用户消息
- function addUserMessage(message) {
- const messageDiv = document.createElement('div');
- messageDiv.className = 'message user';
- messageDiv.innerHTML = `
- <div class="message-content">
- <div class="user-message">
- ${escapeHtml(message)}
- </div>
- </div>
- `;
- messagesDiv.appendChild(messageDiv);
- messages.push({ type: 'user', content: message, timestamp: new Date() });
- scrollToBottom();
- }
- // 添加机器人消息
- function addBotMessage(message) {
- const messageDiv = document.createElement('div');
- messageDiv.className = 'message assistant';
- messageDiv.innerHTML = `
- <div class="message-content">
- <div class="assistant-message">
- <div class="assistant-icon">
- <span style="font-size: 16px;">🤖</span>
- </div>
- <div class="assistant-text">${formatMessage(message)}</div>
- </div>
- </div>
- `;
- messagesDiv.appendChild(messageDiv);
- messages.push({ type: 'assistant', content: message, timestamp: new Date() });
- scrollToBottom();
- }
- function createStreamingBotMessage() {
- const messageDiv = document.createElement('div');
- messageDiv.className = 'message assistant';
- messageDiv.innerHTML = `
- <div class="message-content">
- <div class="assistant-message">
- <div class="assistant-icon">
- <span style="font-size: 16px;">🤖</span>
- </div>
- <div class="assistant-text"></div>
- </div>
- </div>
- `;
- messagesDiv.appendChild(messageDiv);
- messages.push({ type: 'assistant', content: '', timestamp: new Date() });
- scrollToBottom();
- currentAssistantElement = messageDiv.querySelector('.assistant-text');
- currentAssistantText = '';
- }
- function updateStreamingBotMessage(text) {
- if (!currentAssistantElement) {
- createStreamingBotMessage();
- }
- currentAssistantText += text;
- currentAssistantElement.innerHTML = formatMessage(currentAssistantText);
- const lastMessage = messages[messages.length - 1];
- if (lastMessage && lastMessage.type === 'assistant') {
- lastMessage.content = currentAssistantText;
- }
- scrollToBottom();
- }
- async function handleStreamResponse(response) {
- if (!response.ok) {
- hideLoading();
- addBotMessage('❌ 服务器返回错误,请稍后重试。');
- return;
- }
- const reader = response.body?.getReader();
- if (!reader) {
- hideLoading();
- addBotMessage('❌ 当前浏览器不支持流式响应。');
- return;
- }
- createStreamingBotMessage();
- const decoder = new TextDecoder('utf-8');
- let buffer = '';
- let streamFinished = false;
- try {
- while (!streamFinished) {
- const { value, done } = await reader.read();
- if (done) {
- break;
- }
- buffer += decoder.decode(value, { stream: true });
- let boundary = findSseBoundary(buffer);
- while (boundary.index !== -1) {
- const rawEvent = buffer.slice(0, boundary.index).replace(/\r/g, '').trim();
- buffer = buffer.slice(boundary.index + boundary.length);
- streamFinished = processSseEvent(rawEvent) || streamFinished;
- boundary = findSseBoundary(buffer);
- }
- }
- if (buffer.trim()) {
- streamFinished = processSseEvent(buffer.replace(/\r/g, '').trim()) || streamFinished;
- }
- } catch (error) {
- console.error('处理流式响应失败:', error);
- updateStreamingBotMessage('\n❌ 响应解析失败,请稍后重试。');
- } finally {
- hideLoading();
- currentAssistantElement = null;
- currentAssistantText = '';
- }
- }
- function findSseBoundary(text) {
- const lfIndex = text.indexOf('\n\n');
- const crlfIndex = text.indexOf('\r\n\r\n');
- if (lfIndex === -1 && crlfIndex === -1) {
- return { index: -1, length: 0 };
- }
- if (lfIndex === -1) {
- return { index: crlfIndex, length: 4 };
- }
- if (crlfIndex === -1) {
- return { index: lfIndex, length: 2 };
- }
- return lfIndex < crlfIndex
- ? { index: lfIndex, length: 2 }
- : { index: crlfIndex, length: 4 };
- }
- function processSseEvent(rawEvent) {
- if (!rawEvent) return false;
- const lines = rawEvent.split('\n');
- let finished = false;
- for (const line of lines) {
- const trimmedLine = line.trim();
- if (!trimmedLine.startsWith('data:')) continue;
- const dataStr = trimmedLine.slice(5).trim();
- if (!dataStr) continue;
- if (dataStr === '[DONE]') {
- finished = true;
- break;
- }
- let payload;
- try {
- payload = JSON.parse(dataStr);
- } catch (error) {
- console.warn('JSON解析失败,使用原始数据:', dataStr);
- payload = { type: 'text', text: dataStr };
- }
- if (payload.type === 'text' && payload.text) {
- updateStreamingBotMessage(payload.text);
- } else if (payload.type === 'error') {
- updateStreamingBotMessage(`\n❌ ${payload.text || '发生未知错误'}`);
- }
- }
- return finished;
- }
- // 显示加载状态
- function showLoading() {
- isLoading = true;
- loadingMessage.style.display = 'block';
- scrollToBottom();
- }
- // 隐藏加载状态
- function hideLoading() {
- isLoading = false;
- loadingMessage.style.display = 'none';
- }
- // 设置输入状态
- function setInputState(enabled) {
- messageInput.disabled = !enabled;
- sendBtn.disabled = !enabled;
- if (enabled) {
- sendBtn.innerHTML = '↑';
- } else {
- sendBtn.innerHTML = '<div class="loading"></div>';
- }
- }
- // 滚动到底部
- function scrollToBottom() {
- messagesContainer.scrollTop = messagesContainer.scrollHeight;
- }
- // HTML转义
- function escapeHtml(text) {
- const div = document.createElement('div');
- div.textContent = text;
- return div.innerHTML;
- }
- // 格式化消息
- function formatMessage(content) {
- // 先进行HTML转义,再处理换行
- return content.replace(/</g, '<').replace(/>/g, '>').replace(/\n/g, '<br>');
- }
- </script>
- <style>
- * {
- margin: 0;
- padding: 0;
- box-sizing: border-box;
- }
- body {
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
- background: #f8f9fa;
- }
- .warehouse-chat {
- display: flex;
- height: 100vh;
- background: #f8f9fa;
- }
- .sidebar {
- width: 60px;
- background: #ffffff;
- border-right: 1px solid #e5e7eb;
- display: flex;
- flex-direction: column;
- align-items: center;
- padding: 16px 0;
- }
- .logo {
- margin-bottom: 24px;
- }
- .logo-icon {
- width: 32px;
- height: 32px;
- background: linear-gradient(135deg, #f8f8fc, #d6d1df);
- border-radius: 8px;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 16px;
- }
- .nav-buttons {
- display: flex;
- flex-direction: column;
- gap: 8px;
- }
- .nav-btn {
- width: 44px;
- height: 44px;
- border: none;
- background: transparent;
- border-radius: 8px;
- cursor: pointer;
- display: flex;
- align-items: center;
- justify-content: center;
- transition: all 0.2s;
- }
- .nav-btn:hover {
- background: #f3f4f6;
- }
- .nav-btn.active {
- background: #eef2ff;
- color: #4f46e5;
- }
- .nav-icon {
- font-size: 18px;
- }
- .chat-container {
- flex: 1;
- display: flex;
- flex-direction: column;
- max-width: 800px;
- margin: 0 auto;
- width: 100%;
- }
- .welcome-screen {
- flex: 1;
- display: flex;
- align-items: center;
- justify-content: center;
- padding: 40px;
- }
- .welcome-content {
- text-align: center;
- max-width: 600px;
- }
- .welcome-icon {
- font-size: 64px;
- margin-bottom: 24px;
- }
- .welcome-title {
- font-size: 28px;
- font-weight: 600;
- color: #1f2937;
- margin-bottom: 16px;
- line-height: 1.3;
- }
- .welcome-subtitle {
- font-size: 16px;
- color: #6b7280;
- line-height: 1.5;
- margin: 0 0 32px 0;
- }
- .recommendations {
- text-align: left;
- background: white;
- border-radius: 16px;
- padding: 24px;
- box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
- margin-top: 24px;
- }
- .recommendation-section {
- margin-bottom: 20px;
- }
- .recommendation-section h3 {
- font-size: 16px;
- color: #1f2937;
- margin-bottom: 8px;
- }
- .recommendation-section p {
- font-size: 14px;
- color: #6b7280;
- margin: 0;
- }
- .examples {
- display: flex;
- flex-direction: column;
- gap: 12px;
- margin-top: 20px;
- }
- .example {
- background: #f8f9fa;
- border: 1px solid #e5e7eb;
- border-radius: 12px;
- padding: 12px 16px;
- cursor: pointer;
- transition: all 0.2s;
- font-size: 14px;
- color: #374151;
- }
- .example:hover {
- background: #eef2ff;
- border-color: #4f46e5;
- color: #4f46e5;
- }
- .messages-container {
- flex: 1;
- overflow-y: auto;
- padding: 24px;
- padding-bottom: 0;
- }
- .message {
- margin-bottom: 24px;
- }
- .message-content {
- display: flex;
- max-width: 100%;
- }
- .user-message {
- background: #4f46e5;
- color: white;
- padding: 12px 18px;
- border-radius: 18px;
- margin-left: auto;
- max-width: 70%;
- word-wrap: break-word;
- line-height: 1.4;
- }
- .assistant-message {
- display: flex;
- align-items: flex-start;
- gap: 12px;
- max-width: 80%;
- }
- .assistant-icon {
- width: 32px;
- height: 32px;
- background: linear-gradient(135deg, #f5f5f7, #cfd5e1);
- border-radius: 50%;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 14px;
- flex-shrink: 0;
- }
- .assistant-text {
- background: white;
- border: 1px solid #e5e7eb;
- padding: 12px 18px;
- border-radius: 18px;
- line-height: 1.5;
- white-space: pre-line;
- color: #1f2937;
- }
- .typing-indicator {
- display: flex;
- gap: 4px;
- padding: 12px 18px;
- background: white;
- border: 1px solid #e5e7eb;
- border-radius: 18px;
- }
- .dot {
- width: 6px;
- height: 6px;
- background: #9ca3af;
- border-radius: 50%;
- animation: typing 1.4s infinite;
- }
- .dot:nth-child(2) {
- animation-delay: 0.2s;
- }
- .dot:nth-child(3) {
- animation-delay: 0.4s;
- }
- @keyframes typing {
- 0%, 60%, 100% {
- transform: translateY(0);
- }
- 30% {
- transform: translateY(-10px);
- }
- }
- .input-container {
- padding: 24px;
- background: white;
- border-top: 1px solid #e5e7eb;
- }
- .input-box {
- max-width: 100%;
- }
- .input-wrapper {
- position: relative;
- background: white;
- border: 2px solid #e5e7eb;
- border-radius: 24px;
- padding: 8px 16px;
- display: flex;
- align-items: center;
- gap: 8px;
- transition: border-color 0.2s;
- }
- .input-wrapper:focus-within {
- border-color: #4f46e5;
- }
- .message-input {
- flex: 1;
- border: none;
- outline: none;
- font-size: 16px;
- padding: 8px 0;
- background: transparent;
- color: #1f2937;
- }
- .message-input::placeholder {
- color: #9ca3af;
- }
- .input-actions {
- display: flex;
- align-items: center;
- gap: 8px;
- }
- .action-btn {
- width: 32px;
- height: 32px;
- border: none;
- background: transparent;
- border-radius: 6px;
- cursor: pointer;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 14px;
- transition: background 0.2s;
- }
- .action-btn:hover {
- background: #f3f4f6;
- }
- .send-btn {
- width: 32px;
- height: 32px;
- background: #4f46e5;
- border: none;
- border-radius: 50%;
- color: white;
- cursor: pointer;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 16px;
- font-weight: bold;
- transition: all 0.2s;
- }
- .send-btn:hover:not(:disabled) {
- background: #4338ca;
- transform: translateY(-1px);
- }
- .send-btn:disabled {
- background: #d1d5db;
- cursor: not-allowed;
- }
- .disclaimer {
- text-align: center;
- font-size: 12px;
- color: #9ca3af;
- margin-top: 12px;
- margin-bottom: 0;
- }
- /* 响应式设计 */
- @media (max-width: 768px) {
- .sidebar {
- width: 50px;
- padding: 12px 0;
- }
- .welcome-title {
- font-size: 24px;
- }
- .messages-container {
- padding: 16px;
- }
- .input-container {
- padding: 16px;
- }
- .user-message,
- .assistant-message {
- max-width: 90%;
- }
- .examples {
- gap: 8px;
- }
- .example {
- padding: 10px 12px;
- font-size: 13px;
- }
- }
- </style>
- </body>
- </html>
|