Files
leaudit-platform-frontend/app/config/chat.ts
T
TanWenyan 679fa31ce3 重构:客户端直接调用FastAPI后端,不再通过Remix中转
架构变更:
旧架构:浏览器 → Remix /api/* → dify-client.server → FastAPI /dify → Dify
新架构:浏览器 → FastAPI /dify/* → Dify

修改:
1. app/config/chat.ts
   - getApiBaseUrl() 根据端口映射返回FastAPI后端地址
   - 端口映射表:
     * 51703 → http://172.16.0.55:8073
     * 51704 → http://10.79.97.17:8001
     * 51705 → http://10.79.97.17:8002
     * 51706 → http://10.79.97.17:8003
     * 51707 → http://10.79.97.17:8004
     * 51708 → http://10.79.97.17:8005
   - API_URL: `${baseUrl}/dify`

示例(端口51703):
- 浏览器Network: http://172.16.0.55:8073/dify/chat-messages
- 不再经过Remix API routes

注意:
- 需要FastAPI配置CORS允许跨域
- Cookie需要在跨域情况下正确传递

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-30 14:44:49 +08:00

103 lines
3.1 KiB
TypeScript

import type { AppInfo } from '../types/dify_chat';
// 在客户端获取环境变量的辅助函数
const getEnvVar = (name: string, defaultValue: string = '') => {
// 在服务端
if (typeof window === 'undefined') {
return process.env[name] || defaultValue;
}
// 在客户端,从window.__ENV获取
return (window as any).__ENV?.[name] || defaultValue;
};
// 从完整的APP URL中提取APP ID
const extractAppId = (appUrl: string): string => {
if (!appUrl) return '';
// 如果是完整的URL,提取最后的UUID部分
const match = appUrl.match(/\/app\/([a-f0-9-]{36})/);
if (match) {
return match[1];
}
// 如果已经是UUID格式,直接返回
if (/^[a-f0-9-]{36}$/.test(appUrl)) {
return appUrl;
}
return appUrl;
};
// 获取FastAPI后端的基础URL
// 客户端直接调用FastAPI后端,不再通过Remix API routes中转
const getApiBaseUrl = () => {
// 在客户端,从 API_BASE_URL 配置获取后端地址
if (typeof window !== 'undefined') {
// 客户端:需要从某处获取 baseUrl 配置
// 方案1: 从环境变量
// 方案2: 从 window 全局变量
// 方案3: 硬编码端口映射
// 获取当前端口
const port = window.location.port;
// 端口到后端地址的映射(与 api-config.ts 保持一致)
const portToBackend: Record<string, string> = {
'5173': 'http://172.16.0.55:8000',
'51703': 'http://172.16.0.55:8073',
'51704': 'http://10.79.97.17:8001',
'51705': 'http://10.79.97.17:8002',
'51706': 'http://10.79.97.17:8003',
'51707': 'http://10.79.97.17:8004',
'51708': 'http://10.79.97.17:8005',
};
return portToBackend[port] || 'http://172.16.0.55:8000';
}
// 服务端:不应该被调用
return '';
};
// 聊天应用配置
export const CHAT_CONFIG = {
// API相关配置 - 客户端直接调用FastAPI后端的/dify路由
// 客户端 → FastAPI /dify/* → Dify
API_URL: `${getApiBaseUrl()}/dify`,
// 应用ID - 用于localStorage key(固定值)
APP_ID: 'docreview-chat',
// API_KEY 不再需要 - 服务端通过JWT处理认证
API_KEY: '', // 保留字段以兼容旧代码
// 应用信息
APP_INFO: {
title: '大模型对话',
description: '大模型对话',
copyright: '大模型对话',
privacy_policy: '大模型对话',
default_language: 'zh-Hans',
},
// 功能配置
isShowPrompt: false,
promptTemplate: 'I want you to act as a javascript console.',
// 本地化
LOCALE_COOKIE_NAME: 'locale',
// 限制
DEFAULT_VALUE_MAX_LEN: 48,
};
// SSE超时设置
export const SSE_TIMEOUT = 100000;
// 内容类型
export const ContentType = {
json: 'application/json',
stream: 'text/event-stream',
form: 'application/x-www-form-urlencoded; charset=UTF-8',
download: 'application/octet-stream',
};