ui: settings body — providers (APInex) config, local clientStorage persistence, backup/import
This commit is contained in:
1 parent
379b3d3370
commit
1468570d0f
2 files changed
+227
-1
No files matched your search
@@ -0,0 +1,76 @@
|
||||
// Local settings model + persistence (figma.clientStorage).
|
||||
//
|
||||
// No server auth yet — everything lives on the Figma file locally. Backup/restore
|
||||
// is just a JSON export/import of this same document. (Server auth comes later.)
|
||||
|
||||
export type ProviderType = 'apinex'
|
||||
|
||||
export interface ProviderConfig {
|
||||
id: string
|
||||
name: string
|
||||
type: ProviderType
|
||||
baseUrl: string
|
||||
apiKey: string
|
||||
defaultModel: string
|
||||
}
|
||||
|
||||
export interface Settings {
|
||||
version: 1
|
||||
auth: { mode: 'local' } // later: { mode: 'server', ... }
|
||||
providers: ProviderConfig[]
|
||||
activeProviderId: string | null
|
||||
}
|
||||
|
||||
export const APINEX_BASE_URL = 'https://api.apinex.bond/v1'
|
||||
const SETTINGS_KEY = 'settings'
|
||||
|
||||
export function defaultSettings (): Settings {
|
||||
return {
|
||||
version: 1,
|
||||
auth: { mode: 'local' },
|
||||
providers: [],
|
||||
activeProviderId: null
|
||||
}
|
||||
}
|
||||
|
||||
function genId (): string {
|
||||
return 'p_' + Math.random().toString(36).slice(2, 10)
|
||||
}
|
||||
|
||||
export function createApinexProvider (): ProviderConfig {
|
||||
return {
|
||||
id: genId(),
|
||||
name: 'APInex',
|
||||
type: 'apinex',
|
||||
baseUrl: APINEX_BASE_URL,
|
||||
apiKey: '',
|
||||
defaultModel: ''
|
||||
}
|
||||
}
|
||||
|
||||
export async function loadSettings (): Promise<Settings> {
|
||||
try {
|
||||
const stored = await figma.clientStorage.getAsync(SETTINGS_KEY)
|
||||
if (stored == null || stored.version !== 1) return defaultSettings()
|
||||
return stored as Settings
|
||||
} catch (e) {
|
||||
return defaultSettings()
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveSettings (settings: Settings): Promise<void> {
|
||||
try {
|
||||
await figma.clientStorage.setAsync(SETTINGS_KEY, settings)
|
||||
} catch (e) { /* storage unavailable — ignore */ }
|
||||
}
|
||||
|
||||
export function settingsToJSON (settings: Settings): string {
|
||||
return JSON.stringify(settings, null, 2)
|
||||
}
|
||||
|
||||
export function settingsFromJSON (text: string): Settings {
|
||||
const parsed = JSON.parse(text)
|
||||
if (parsed == null || parsed.version !== 1) throw new Error('Unsupported settings version')
|
||||
if (!Array.isArray(parsed.providers)) throw new Error('Invalid settings: providers must be an array')
|
||||
return parsed as Settings
|
||||
}
|
||||
Reference in new issue
Block a user