@@ -0,0 +1,41 @@
|
||||
import { DEFAULT_AI_SETTINGS, type AISettings } from "./types"
|
||||
|
||||
const STORAGE_KEY = "devvault.ai-settings"
|
||||
|
||||
export function loadAISettings(): AISettings {
|
||||
if (typeof window === "undefined") return { ...DEFAULT_AI_SETTINGS }
|
||||
try {
|
||||
const raw = window.localStorage.getItem(STORAGE_KEY)
|
||||
if (!raw) return { ...DEFAULT_AI_SETTINGS }
|
||||
const parsed = JSON.parse(raw) as Partial<AISettings>
|
||||
return { ...DEFAULT_AI_SETTINGS, ...parsed }
|
||||
} catch {
|
||||
return { ...DEFAULT_AI_SETTINGS }
|
||||
}
|
||||
}
|
||||
|
||||
export function saveAISettings(settings: AISettings): void {
|
||||
if (typeof window === "undefined") return
|
||||
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(settings))
|
||||
}
|
||||
|
||||
export function clearApiKey(): AISettings {
|
||||
const current = loadAISettings()
|
||||
const next = { ...current, apiKey: "" }
|
||||
saveAISettings(next)
|
||||
return next
|
||||
}
|
||||
|
||||
export function resetAISettings(): AISettings {
|
||||
const next = { ...DEFAULT_AI_SETTINGS }
|
||||
saveAISettings(next)
|
||||
return next
|
||||
}
|
||||
|
||||
/** Returns an error message if the settings are not usable for a request, otherwise null. */
|
||||
export function validateForRequest(settings: AISettings): string | null {
|
||||
if (!settings.baseUrl.trim()) return "请先在设置中填写 API Base URL"
|
||||
if (!settings.model.trim()) return "请先在设置中填写 Model"
|
||||
if (!settings.apiKey.trim()) return "请先在设置中填写 API Key"
|
||||
return null
|
||||
}
|
||||
Reference in New Issue
Block a user