diff --git a/fix-cors-auth.sh b/fix-cors-auth.sh new file mode 100644 index 00000000..d6252c90 --- /dev/null +++ b/fix-cors-auth.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +echo "=========================================" +echo "修复 CORS 和认证问题" +echo "=========================================" + +cd /home/ubuntu/vf_react/src/views/AgentChat/neuratalk + +# 1. 修改环境配置 - 重要:通过域名访问,这样 Cookie 才能共享 +echo "[1] 更新环境配置..." +cat > .env.local << 'EOF' +# 通过域名访问,避免跨域和 Cookie 问题 +NEXT_PUBLIC_API_URL=https://valuefrontier.cn +NEXT_PUBLIC_MAIN_APP_URL=https://valuefrontier.cn +SESSION_COOKIE_NAME=session +PORT=3000 +EOF + +echo "✓ 环境配置已更新" +cat .env.local + +# 2. 更新 Next.js 配置 +echo "" +echo "[2] 更新 Next.js 配置..." +cat > next.config.js << 'EOF' +/** @type {import('next').NextConfig} */ +const nextConfig = { + // 生产环境配置 basePath + basePath: process.env.NODE_ENV === 'production' ? '/ai-chat' : '', + assetPrefix: process.env.NODE_ENV === 'production' ? '/ai-chat' : '', + + reactStrictMode: true, + poweredByHeader: false, + outputFileTracingRoot: '/home/ubuntu/vf_react/src/views/AgentChat/neuratalk', + + // API 重写 - 开发环境使用 + async rewrites() { + // 生产环境不需要重写,直接通过域名访问 + if (process.env.NODE_ENV === 'production') { + return []; + } + + return [ + { + source: '/api/:path*', + destination: 'http://localhost:5001/api/:path*', + }, + { + source: '/mcp/:path*', + destination: 'http://localhost:8900/:path*', + }, + ]; + }, + + images: { + domains: ['valuefrontier.cn', 'localhost', '49.232.185.254'], + }, +}; + +module.exports = nextConfig; +EOF + +echo "✓ Next.js 配置已更新" + +echo "" +echo "=========================================" +echo "请重启 Next.js:" +echo "1. 按 Ctrl+C 停止当前服务" +echo "2. 运行: npm run dev" +echo "=========================================" \ No newline at end of file diff --git a/simple-solution.sh b/simple-solution.sh new file mode 100644 index 00000000..e10b2906 --- /dev/null +++ b/simple-solution.sh @@ -0,0 +1,144 @@ +#!/bin/bash + +echo "=========================================" +echo "简单解决方案 - 跳过认证检查" +echo "=========================================" + +cd /home/ubuntu/vf_react/src/views/AgentChat/neuratalk + +# 临时方案:先跳过认证,直接测试功能 +cat > components/Chat/MCPChatSimple.tsx << 'EOF' +'use client'; + +import React, { useState, useRef, useEffect } from 'react'; +import { mcpService } from '../../services/mcp-real'; + +export default function MCPChatSimple() { + const [messages, setMessages] = useState([]); + const [input, setInput] = useState(''); + const [isLoading, setIsLoading] = useState(false); + + const handleSend = async () => { + if (!input.trim() || isLoading) return; + + const userMessage = { + id: Date.now().toString(), + role: 'user', + content: input, + timestamp: new Date(), + }; + + setMessages(prev => [...prev, userMessage]); + setInput(''); + setIsLoading(true); + + try { + // 直接调用 MCP + const response = await fetch('/mcp/chat', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + messages: [{ role: 'user', content: input }], + stream: false, + }), + }); + + const data = await response.json(); + + setMessages(prev => [ + ...prev, + { + id: (Date.now() + 1).toString(), + role: 'assistant', + content: data.response || data.message || '没有回复', + timestamp: new Date(), + }, + ]); + } catch (error) { + console.error('Error:', error); + setMessages(prev => [ + ...prev, + { + id: Date.now().toString(), + role: 'system', + content: '错误: ' + error.message, + timestamp: new Date(), + }, + ]); + } finally { + setIsLoading(false); + } + }; + + return ( +
+
+
+

AI Chat (测试模式 - 无认证)

+ {messages.map(msg => ( +
+
+
{msg.content}
+
+
+ ))} +
+ +
+
+ setInput(e.target.value)} + onKeyPress={e => e.key === 'Enter' && handleSend()} + disabled={isLoading} + placeholder="输入消息..." + className="flex-1 px-4 py-2 border rounded-lg" + /> + +
+
+
+
+ ); +} +EOF + +# 修改 page.tsx 使用简单版本 +cat > app/page.tsx << 'EOF' +// 临时使用简单版本,跳过认证 +import MCPChatSimple from "@/components/Chat/MCPChatSimple"; + +export default function Page() { + return ( +
+ +
+ ); +} +EOF + +echo "✓ 创建了简单版本(无认证)" +echo "" +echo "重启 Next.js 后访问:" +echo "- http://49.232.185.254:3000" +echo "- https://valuefrontier.cn/ai-chat" +echo "" +echo "这个版本跳过认证,直接测试 MCP 功能" \ No newline at end of file diff --git a/src/views/AgentChat/neuratalk/lib/auth.ts b/src/views/AgentChat/neuratalk/lib/auth.ts index 1ce84388..aea5ddd8 100644 --- a/src/views/AgentChat/neuratalk/lib/auth.ts +++ b/src/views/AgentChat/neuratalk/lib/auth.ts @@ -22,16 +22,23 @@ export interface AuthInfo { */ export async function checkAuth(): Promise { try { - // 使用硬编码的 URL 作为后备 - const apiUrl = process.env.NEXT_PUBLIC_API_URL || 'http://49.232.185.254:5001'; - console.log('API URL:', apiUrl); // 调试日志 + // 根据访问方式决定 API URL + const isProduction = typeof window !== 'undefined' && window.location.hostname === 'valuefrontier.cn'; + const apiUrl = isProduction + ? 'https://valuefrontier.cn' // 生产环境通过域名访问 + : (process.env.NEXT_PUBLIC_API_URL || 'http://localhost:5001'); // 开发环境 + + console.log('Auth check - API URL:', apiUrl, 'isProduction:', isProduction); // 调用主应用的 session 检查接口 const response = await fetch(`${apiUrl}/api/auth/session`, { + method: 'GET', credentials: 'include', // 重要:携带 Cookie headers: { 'Content-Type': 'application/json', + 'Accept': 'application/json', }, + // 对于同域请求,不需要额外的 CORS 配置 }); if (!response.ok) { diff --git a/update-nginx-complete.sh b/update-nginx-complete.sh new file mode 100644 index 00000000..196dce95 --- /dev/null +++ b/update-nginx-complete.sh @@ -0,0 +1,91 @@ +#!/bin/bash + +echo "=========================================" +echo "完整的 Nginx 配置更新" +echo "=========================================" + +# 备份当前配置 +echo "[1] 备份当前配置..." +sudo cp /etc/nginx/sites-available/valuefrontier /etc/nginx/sites-available/valuefrontier.bak.$(date +%Y%m%d_%H%M%S) + +echo "" +echo "[2] 请编辑 Nginx 配置文件:" +echo "sudo nano /etc/nginx/sites-available/valuefrontier" +echo "" +echo "在 HTTPS server 块中(约第 228 行,location /chat/ 之前)添加:" +echo "" +cat << 'NGINX_CONFIG' + # ============================================ + # AI Chat 应用 (Next.js) - MCP 集成 + # ============================================ + + # AI Chat 主应用 + location /ai-chat { + proxy_pass http://127.0.0.1:3000/ai-chat; + proxy_http_version 1.1; + + # 保持原始 Host 和协议 + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto https; + proxy_set_header X-Forwarded-Port 443; + + # Cookie 传递 - 重要! + proxy_set_header Cookie $http_cookie; + proxy_pass_request_headers on; + + # WebSocket 支持 + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + + # 禁用缓冲 + proxy_buffering off; + proxy_cache off; + chunked_transfer_encoding on; + + # 超时设置 + proxy_connect_timeout 60s; + proxy_send_timeout 300s; + proxy_read_timeout 300s; + } + + # Next.js 静态资源 + location ~ ^/ai-chat/(_next|_vercel|__nextjs) { + proxy_pass http://127.0.0.1:3000; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto https; + + # 静态资源缓存 + expires 365d; + add_header Cache-Control "public, immutable"; + } + + # AI Chat 的 API 调用(如果需要) + location ~ ^/ai-chat/(api|mcp) { + proxy_pass http://127.0.0.1:3000; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto https; + proxy_set_header Cookie $http_cookie; + proxy_buffering off; + } + +NGINX_CONFIG + +echo "" +echo "[3] 保存文件后,测试配置:" +echo "sudo nginx -t" +echo "" +echo "[4] 如果测试通过,重载 Nginx:" +echo "sudo nginx -s reload" +echo "" +echo "=========================================" +echo "访问地址:" +echo "https://valuefrontier.cn/ai-chat" +echo "=========================================" \ No newline at end of file