|
| 1 | +import { |
| 2 | + BaseDirectory, |
| 3 | + exists, |
| 4 | + readTextFile, |
| 5 | + writeTextFile, |
| 6 | +} from "@tauri-apps/plugin-fs"; |
| 7 | +import { createCustomPersister } from "tinybase/persisters/with-schemas"; |
| 8 | +import type { |
| 9 | + Content, |
| 10 | + MergeableStore, |
| 11 | + OptionalSchemas, |
| 12 | +} from "tinybase/with-schemas"; |
| 13 | + |
| 14 | +import { StoreOrMergeableStore } from "./shared"; |
| 15 | + |
| 16 | +type ProviderType = "llm" | "stt"; |
| 17 | +type ProviderData = { base_url: string; api_key: string }; |
| 18 | +export type SimplifiedFormat = Record< |
| 19 | + ProviderType, |
| 20 | + Record<string, ProviderData> |
| 21 | +>; |
| 22 | + |
| 23 | +export function toSimplifiedFormat<Schemas extends OptionalSchemas>( |
| 24 | + store: MergeableStore<Schemas>, |
| 25 | +): SimplifiedFormat { |
| 26 | + const result: SimplifiedFormat = { llm: {}, stt: {} }; |
| 27 | + const rows = store.getTable("ai_providers") ?? {}; |
| 28 | + |
| 29 | + for (const [rowId, row] of Object.entries(rows)) { |
| 30 | + const { type, base_url, api_key } = row as unknown as { |
| 31 | + type: ProviderType; |
| 32 | + base_url: string; |
| 33 | + api_key: string; |
| 34 | + }; |
| 35 | + if (type === "llm" || type === "stt") { |
| 36 | + result[type][rowId] = { base_url, api_key }; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return result; |
| 41 | +} |
| 42 | + |
| 43 | +export function fromSimplifiedFormat<Schemas extends OptionalSchemas>( |
| 44 | + data: SimplifiedFormat, |
| 45 | +): Content<Schemas> { |
| 46 | + const aiProviders: Record< |
| 47 | + string, |
| 48 | + { type: string; base_url: string; api_key: string } |
| 49 | + > = {}; |
| 50 | + |
| 51 | + for (const providerType of ["llm", "stt"] as const) { |
| 52 | + const providers = data[providerType] ?? {}; |
| 53 | + for (const [providerId, providerData] of Object.entries(providers)) { |
| 54 | + aiProviders[providerId] = { |
| 55 | + type: providerType, |
| 56 | + base_url: providerData.base_url ?? "", |
| 57 | + api_key: providerData.api_key ?? "", |
| 58 | + }; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return [{ ai_providers: aiProviders }, {}] as unknown as Content<Schemas>; |
| 63 | +} |
| 64 | + |
| 65 | +export function createJsonPersister<Schemas extends OptionalSchemas>( |
| 66 | + store: MergeableStore<Schemas>, |
| 67 | + filename: string, |
| 68 | +) { |
| 69 | + const path = `hyprnote/${filename}`; |
| 70 | + const options = { baseDir: BaseDirectory.Data }; |
| 71 | + |
| 72 | + return createCustomPersister( |
| 73 | + store, |
| 74 | + async (): Promise<Content<Schemas> | undefined> => { |
| 75 | + if (!(await exists(path, options))) { |
| 76 | + return undefined; |
| 77 | + } |
| 78 | + const content = await readTextFile(path, options); |
| 79 | + return fromSimplifiedFormat<Schemas>(JSON.parse(content)); |
| 80 | + }, |
| 81 | + async () => { |
| 82 | + const data = toSimplifiedFormat(store); |
| 83 | + await writeTextFile(path, JSON.stringify(data, null, 2), options); |
| 84 | + }, |
| 85 | + (listener) => setInterval(listener, 1000), |
| 86 | + (handle) => clearInterval(handle), |
| 87 | + (error) => console.error(`[JsonPersister] ${filename}:`, error), |
| 88 | + StoreOrMergeableStore, |
| 89 | + ); |
| 90 | +} |
0 commit comments