137 lines
3.2 KiB
TypeScript
137 lines
3.2 KiB
TypeScript
|
|
/**
|
||
|
|
* localStorage 工具函數
|
||
|
|
* 用於管理 AI Token 和 Provider 設定
|
||
|
|
*/
|
||
|
|
|
||
|
|
const STORAGE_KEYS = {
|
||
|
|
GEMINI_TOKEN: 'ai_token_gemini',
|
||
|
|
GROK_TOKEN: 'ai_token_grok',
|
||
|
|
PROVIDER: 'ai_provider',
|
||
|
|
TEXT_MODEL: 'ai_text_model',
|
||
|
|
VISION_MODEL: 'ai_vision_model',
|
||
|
|
IMAGE_GENERATION_MODEL: 'ai_image_generation_model',
|
||
|
|
TTS_MODEL: 'ai_tts_model'
|
||
|
|
} as const
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 取得 Gemini Token
|
||
|
|
*/
|
||
|
|
export function getGeminiToken(): string | null {
|
||
|
|
if (typeof window === 'undefined') return null
|
||
|
|
return localStorage.getItem(STORAGE_KEYS.GEMINI_TOKEN)
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 設定 Gemini Token
|
||
|
|
*/
|
||
|
|
export function setGeminiToken(token: string): void {
|
||
|
|
if (typeof window === 'undefined') return
|
||
|
|
localStorage.setItem(STORAGE_KEYS.GEMINI_TOKEN, token)
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 取得 Grok Token
|
||
|
|
*/
|
||
|
|
export function getGrokToken(): string | null {
|
||
|
|
if (typeof window === 'undefined') return null
|
||
|
|
return localStorage.getItem(STORAGE_KEYS.GROK_TOKEN)
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 設定 Grok Token
|
||
|
|
*/
|
||
|
|
export function setGrokToken(token: string): void {
|
||
|
|
if (typeof window === 'undefined') return
|
||
|
|
localStorage.setItem(STORAGE_KEYS.GROK_TOKEN, token)
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 取得當前 Provider
|
||
|
|
*/
|
||
|
|
export function getProvider(): 'gemini' | 'grok' {
|
||
|
|
if (typeof window === 'undefined') return 'gemini'
|
||
|
|
const provider = localStorage.getItem(STORAGE_KEYS.PROVIDER)
|
||
|
|
return (provider === 'grok' ? 'grok' : 'gemini') as 'gemini' | 'grok'
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 設定 Provider
|
||
|
|
*/
|
||
|
|
export function setProvider(provider: 'gemini' | 'grok'): void {
|
||
|
|
if (typeof window === 'undefined') return
|
||
|
|
localStorage.setItem(STORAGE_KEYS.PROVIDER, provider)
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 取得當前 Provider 的 Token
|
||
|
|
*/
|
||
|
|
export function getCurrentProviderToken(): string | null {
|
||
|
|
const provider = getProvider()
|
||
|
|
return provider === 'gemini' ? getGeminiToken() : getGrokToken()
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 取得文字模型
|
||
|
|
*/
|
||
|
|
export function getTextModel(): string | null {
|
||
|
|
if (typeof window === 'undefined') return null
|
||
|
|
return localStorage.getItem(STORAGE_KEYS.TEXT_MODEL)
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 設定文字模型
|
||
|
|
*/
|
||
|
|
export function setTextModel(model: string): void {
|
||
|
|
if (typeof window === 'undefined') return
|
||
|
|
localStorage.setItem(STORAGE_KEYS.TEXT_MODEL, model)
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 取得視覺模型
|
||
|
|
*/
|
||
|
|
export function getVisionModel(): string | null {
|
||
|
|
if (typeof window === 'undefined') return null
|
||
|
|
return localStorage.getItem(STORAGE_KEYS.VISION_MODEL)
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 設定視覺模型
|
||
|
|
*/
|
||
|
|
export function setVisionModel(model: string): void {
|
||
|
|
if (typeof window === 'undefined') return
|
||
|
|
localStorage.setItem(STORAGE_KEYS.VISION_MODEL, model)
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 取得圖像生成模型
|
||
|
|
*/
|
||
|
|
export function getImageGenerationModel(): string | null {
|
||
|
|
if (typeof window === 'undefined') return null
|
||
|
|
return localStorage.getItem(STORAGE_KEYS.IMAGE_GENERATION_MODEL)
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 設定圖像生成模型
|
||
|
|
*/
|
||
|
|
export function setImageGenerationModel(model: string): void {
|
||
|
|
if (typeof window === 'undefined') return
|
||
|
|
localStorage.setItem(STORAGE_KEYS.IMAGE_GENERATION_MODEL, model)
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 取得 TTS 模型
|
||
|
|
*/
|
||
|
|
export function getTTSModel(): string | null {
|
||
|
|
if (typeof window === 'undefined') return null
|
||
|
|
return localStorage.getItem(STORAGE_KEYS.TTS_MODEL)
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 設定 TTS 模型
|
||
|
|
*/
|
||
|
|
export function setTTSModel(model: string): void {
|
||
|
|
if (typeof window === 'undefined') return
|
||
|
|
localStorage.setItem(STORAGE_KEYS.TTS_MODEL, model)
|
||
|
|
}
|
||
|
|
|