cf6e9c2421
根本问题:客户端代码直接调用Dify API(12980端口),绕过了服务端代理 修改内容: 1. app/config/api-config.ts - 添加独立的 difyBaseUrl 配置(指向外网 nas.7bm.co:8000) - 导出 DIFY_BASE_URL 供服务端使用 2. app/config/chat.ts - 移除直接Dify API配置(NEXT_PUBLIC_API_URL, APP_ID, API_KEY) - 移除 generateUserId 函数 - API_URL 改为 '/api'(指向Remix API routes) 3. app/services/api.client.ts - 所有fetch调用改为相对路径 /api/* - 移除所有 Authorization 头(服务端自动处理JWT) - 移除所有 user 参数传递(服务端从JWT提取) - credentials 改为 'include' 以携带cookie 4. app/services/dify-client.server.ts - 使用 DIFY_BASE_URL 替代 API_BASE_URL 5. app/utils/dify-test.client.ts - 测试函数改为调用Remix API routes 调用链路: 客户端 → /api/* → Remix API routes → dify-client.server.ts → FastAPI /dify → Dify 解决问题: - ✅ 不再直接调用 nas.7bm.co:12980(Dify端口) - ✅ 统一通过 nas.7bm.co:8000/dify(FastAPI代理) - ✅ 所有请求都经过JWT认证 - ✅ user字段由后端自动管理 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
74 lines
2.2 KiB
TypeScript
74 lines
2.2 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;
|
||
};
|
||
|
||
// 获取Remix API路由的基础URL(客户端调用服务端API routes)
|
||
// 注意:客户端不再直接调用Dify API,而是调用Remix的API routes
|
||
// 服务端的API routes会通过dify-client.server.ts代理到FastAPI,再到Dify
|
||
const getApiBaseUrl = () => {
|
||
// 客户端使用相对路径调用Remix API routes
|
||
return '/api';
|
||
};
|
||
|
||
// 聊天应用配置
|
||
export const CHAT_CONFIG = {
|
||
// API相关配置 - 调用Remix API routes(不再直接调用Dify)
|
||
// 客户端 → /api/* routes → dify-client.server.ts → FastAPI /dify → Dify
|
||
API_URL: getApiBaseUrl(),
|
||
|
||
// 应用信息
|
||
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',
|
||
};
|