Files
leaudit-platform-frontend/app/config/chat.ts
T
2025-06-04 11:18:52 +08:00

98 lines
2.7 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;
};
// 获取配置值并添加调试日志
const getApiUrl = () => {
// 在Remix中,我们使用本地API路由作为代理,而不是直接访问Dify API
if (typeof window !== 'undefined') {
// 客户端:使用相对路径访问本地API
return '/api';
} else {
// 服务端:也使用相对路径
return '/api';
}
};
const getAppId = () => {
const rawAppId = getEnvVar('NEXT_PUBLIC_APP_ID', '');
const extractedAppId = extractAppId(rawAppId);
// console.log('🔧 Chat Config Debug:', {
// rawAppId,
// extractedAppId,
// apiUrl: getApiUrl(),
// hasApiKey: !!getEnvVar('NEXT_PUBLIC_APP_KEY', ''),
// difyApiUrl: getEnvVar('NEXT_PUBLIC_API_URL', ''),
// });
return extractedAppId;
};
// 聊天应用配置
export const CHAT_CONFIG = {
// API相关配置 - 使用本地API路由作为代理
API_URL: getApiUrl(),
APP_ID: getAppId(),
API_KEY: getEnvVar('NEXT_PUBLIC_APP_KEY', ''),
// Dify API 配置(用于服务端)
DIFY_API_URL: getEnvVar('NEXT_PUBLIC_API_URL', 'https://api.dify.ai/v1'),
// 应用信息
APP_INFO: {
title: '大模型对话',
description: '大模型对话',
copyright: '大模型对话',
privacy_policy: '大模型对话',
default_language: 'zh-Hans',
},
// 功能配置
isShowPrompt: false,
promptTemplate: 'I want you to act as a javascript console.',
// API相关
API_PREFIX: '/api',
// 本地化
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',
};