Skip to content

Commit 9842a81

Browse files
committed
refactor(ui): 使用 VITE_LOCAL_DEV 环境变量统一判断开发模式
- 新增 Core 层 isDevelopment() 函数,通过 getEnvVar 动态访问环境变量 - useProSubMode 根据开发模式动态设置默认子模式 - App.vue 使用 isDevelopment() 控制系统提示词优化选项显示 - 避免 Vite 编译时内联替换,确保跨环境兼容性
1 parent 8524d07 commit 9842a81

File tree

5 files changed

+50
-19
lines changed

5 files changed

+50
-19
lines changed

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export {
122122
isElectronApiReady,
123123
waitForElectronApi,
124124
isBrowser,
125+
isDevelopment,
125126
getEnvVar,
126127
scanCustomModelEnvVars,
127128
clearCustomModelEnvCache,

packages/core/src/utils/environment.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ export const isBrowser = (): boolean => {
114114
return typeof window !== 'undefined';
115115
};
116116

117+
/**
118+
* 检查是否在开发模式
119+
* 使用统一的 VITE_LOCAL_DEV 环境变量判断,避免依赖 NODE_ENV、MODE 等内置环境变量
120+
* 通过 getEnvVar 动态访问,避免 Vite 编译时内联替换(类似 VITE_APP_PLATFORM 的设计)
121+
*
122+
* 只有当 VITE_LOCAL_DEV 环境变量显式设置为 'true' 时才认为是开发环境
123+
* 支持多种环境:Vite、Node.js、Docker、Electron等
124+
*/
125+
export function isDevelopment(): boolean {
126+
// 只检查 VITE_LOCAL_DEV 环境变量
127+
const localDev = getEnvVar('VITE_LOCAL_DEV');
128+
return localDev === 'true';
129+
}
130+
117131

118132
/**
119133
* 检测是否在Electron环境中运行

packages/extension/src/App.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<OptimizationModeSelectorUI
3939
v-if="functionMode === 'pro'"
4040
:modelValue="proSubMode"
41-
:hide-system-option="true"
41+
:hide-system-option="!isDev"
4242
@change="handleProSubModeChange"
4343
/>
4444

@@ -1043,6 +1043,7 @@ import type {
10431043
Template,
10441044
ModelConfig,
10451045
} from "@prompt-optimizer/core";
1046+
import { isDevelopment } from "@prompt-optimizer/core";
10461047
import type {
10471048
ModelSelectOption,
10481049
TemplateSelectOption,
@@ -1055,6 +1056,9 @@ const hljsInstance = hljs;
10551056
const { t } = useI18n();
10561057
const toast = useToast();
10571058
1059+
// 环境变量:是否为开发模式(使用统一的 isDevelopment() 函数)
1060+
const isDev = isDevelopment();
1061+
10581062
// 2. 初始化应用服务
10591063
const { services, isInitializing } = useAppInitializer();
10601064

packages/ui/src/composables/mode/useProSubMode.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ref, readonly, type Ref } from 'vue'
22

33
import type { AppServices } from '../../types/services'
44
import { usePreferences } from '../storage/usePreferenceManager'
5-
import { UI_SETTINGS_KEYS, type ProSubMode } from '@prompt-optimizer/core'
5+
import { UI_SETTINGS_KEYS, type ProSubMode, isDevelopment } from '@prompt-optimizer/core'
66

77
interface UseProSubModeApi {
88
proSubMode: Ref<ProSubMode>
@@ -12,6 +12,10 @@ interface UseProSubModeApi {
1212
ensureInitialized: () => Promise<void>
1313
}
1414

15+
// 根据 VITE_LOCAL_DEV 环境变量确定默认模式:开发模式启用 system,生产模式使用 user
16+
// 使用 Core 层的 isDevelopment() 统一判断
17+
const DEFAULT_PRO_SUB_MODE: ProSubMode = isDevelopment() ? 'system' : 'user'
18+
1519
let singleton: {
1620
mode: Ref<ProSubMode>
1721
initialized: boolean
@@ -20,14 +24,18 @@ let singleton: {
2024

2125
/**
2226
* 上下文模式(Pro模式)的子模式单例。读取/写入 PreferenceService。
23-
* - 默认值为 'user'(用户提示词优化)
24-
* - 系统提示词优化暂时隐藏,切换到 Pro 模式时强制使用 'user'
27+
* - 默认值根据 VITE_LOCAL_DEV 环境变量判断:开发模式为 'system',生产模式为 'user'
28+
* - 系统提示词优化仅在开发模式(VITE_LOCAL_DEV=true)下可用
2529
* - 第一次调用时异步初始化
2630
* - 状态独立于基础模式,实现不同功能模式下的子模式状态隔离
2731
*/
2832
export function useProSubMode(services: Ref<AppServices | null>): UseProSubModeApi {
2933
if (!singleton) {
30-
singleton = { mode: ref<ProSubMode>('user'), initialized: false, initializing: null }
34+
singleton = {
35+
mode: ref<ProSubMode>(DEFAULT_PRO_SUB_MODE),
36+
initialized: false,
37+
initializing: null
38+
}
3139
}
3240

3341
const { getPreference, setPreference } = usePreferences(services)
@@ -40,31 +48,31 @@ export function useProSubMode(services: Ref<AppServices | null>): UseProSubModeA
4048
}
4149
singleton!.initializing = (async () => {
4250
try {
43-
// 读取 pro-sub-mode;若不存在,返回默认 'user'
44-
const saved = await getPreference<ProSubMode>(UI_SETTINGS_KEYS.PRO_SUB_MODE, 'user')
51+
// 读取 pro-sub-mode;若不存在,返回默认值(VITE_LOCAL_DEV=true:system,其他:user
52+
const saved = await getPreference<ProSubMode>(UI_SETTINGS_KEYS.PRO_SUB_MODE, DEFAULT_PRO_SUB_MODE)
4553

46-
// 强制使用 'user' 模式(临时禁用 'system' 模式)
47-
// 如果之前保存的是 'system',自动切换为 'user'
48-
if (saved === 'system') {
54+
// 非开发模式(VITE_LOCAL_DEV≠true)下强制使用 'user' 模式(禁用 'system' 模式)
55+
// 开发模式(VITE_LOCAL_DEV=true)下允许使用 'system' 模式
56+
if (!isDevelopment() && saved === 'system') {
4957
singleton!.mode.value = 'user'
5058
await setPreference(UI_SETTINGS_KEYS.PRO_SUB_MODE, 'user')
51-
console.log('[useProSubMode] 检测到旧的 system 模式,已强制切换为 user')
59+
console.log('[useProSubMode] 生产模式检测到 system 模式,已强制切换为 user')
5260
} else {
53-
singleton!.mode.value = (saved === 'user') ? saved : 'user'
61+
singleton!.mode.value = (saved === 'system' || saved === 'user') ? saved : DEFAULT_PRO_SUB_MODE
5462
}
5563

5664
console.log(`[useProSubMode] 初始化完成,当前值: ${singleton!.mode.value}`)
5765

5866
// 将默认值持久化(若未设置过或值无效)
59-
if (saved !== 'user') {
60-
await setPreference(UI_SETTINGS_KEYS.PRO_SUB_MODE, 'user')
61-
console.log('[useProSubMode] 已持久化默认值: user')
67+
if (saved !== 'system' && saved !== 'user') {
68+
await setPreference(UI_SETTINGS_KEYS.PRO_SUB_MODE, DEFAULT_PRO_SUB_MODE)
69+
console.log(`[useProSubMode] 已持久化默认值: ${DEFAULT_PRO_SUB_MODE}`)
6270
}
6371
} catch (e) {
64-
console.error('[useProSubMode] 初始化失败,使用默认值 user:', e)
65-
// 读取失败则保持默认 'user',并尝试持久化
72+
console.error(`[useProSubMode] 初始化失败,使用默认值 ${DEFAULT_PRO_SUB_MODE}:`, e)
73+
// 读取失败则保持默认值,并尝试持久化
6674
try {
67-
await setPreference(UI_SETTINGS_KEYS.PRO_SUB_MODE, 'user')
75+
await setPreference(UI_SETTINGS_KEYS.PRO_SUB_MODE, DEFAULT_PRO_SUB_MODE)
6876
} catch {
6977
// 忽略设置失败错误
7078
}

packages/web/src/App.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<OptimizationModeSelectorUI
3939
v-if="functionMode === 'pro'"
4040
:modelValue="proSubMode"
41-
:hide-system-option="true"
41+
:hide-system-option="!isDev"
4242
@change="handleProSubModeChange"
4343
/>
4444

@@ -1043,6 +1043,7 @@ import type {
10431043
Template,
10441044
ModelConfig,
10451045
} from "@prompt-optimizer/core";
1046+
import { isDevelopment } from "@prompt-optimizer/core";
10461047
import type {
10471048
ModelSelectOption,
10481049
TemplateSelectOption,
@@ -1055,6 +1056,9 @@ const hljsInstance = hljs;
10551056
const { t } = useI18n();
10561057
const toast = useToast();
10571058
1059+
// 环境变量:是否为开发模式(使用统一的 isDevelopment() 函数)
1060+
const isDev = isDevelopment();
1061+
10581062
// 2. 初始化应用服务
10591063
const { services, isInitializing } = useAppInitializer();
10601064

0 commit comments

Comments
 (0)