102 lines
2.9 KiB
TypeScript
102 lines
2.9 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;
|
|
};
|
|
|
|
// 获取Dify API配置
|
|
const getDifyApiUrl = () => {
|
|
return getEnvVar('NEXT_PUBLIC_API_URL', 'https://api.dify.ai/v1');
|
|
};
|
|
|
|
const getAppId = () => {
|
|
const rawAppId = getEnvVar('NEXT_PUBLIC_APP_ID', '');
|
|
const extractedAppId = extractAppId(rawAppId);
|
|
// console.log('🔧 Chat Config Debug:', {
|
|
// rawAppId,
|
|
// extractedAppId,
|
|
// difyApiUrl: getDifyApiUrl(),
|
|
// hasApiKey: !!getEnvVar('NEXT_PUBLIC_APP_KEY', ''),
|
|
// });
|
|
return extractedAppId;
|
|
};
|
|
|
|
// 生成用户ID (模拟服务端逻辑)
|
|
const generateUserId = () => {
|
|
const appId = getAppId();
|
|
// 生成或获取会话ID (可以使用localStorage或其他方式)
|
|
let sessionId = '';
|
|
if (typeof window !== 'undefined') {
|
|
sessionId = localStorage.getItem('dify_session_id') || '';
|
|
if (!sessionId) {
|
|
sessionId = 'sess_' + Math.random().toString(36).substring(2, 15);
|
|
localStorage.setItem('dify_session_id', sessionId);
|
|
}
|
|
}
|
|
return `user_${appId}:${sessionId}`;
|
|
};
|
|
|
|
// 聊天应用配置
|
|
export const CHAT_CONFIG = {
|
|
// API相关配置 - 直接使用Dify API
|
|
API_URL: getDifyApiUrl(),
|
|
APP_ID: getAppId(),
|
|
API_KEY: getEnvVar('NEXT_PUBLIC_APP_KEY', ''),
|
|
|
|
// 用户生成函数
|
|
generateUserId,
|
|
|
|
// 应用信息
|
|
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',
|
|
};
|