| 1 | import { app } from "./bridge"; |
| 2 | import type { ModelSettingsChange, ModelSettingsResult, SettingsView } from "./types"; |
| 3 | |
| 4 | type WithoutRequest<T> = T extends unknown ? Omit<T, "requestId" | "expectedFingerprint"> : never; |
| 5 | export type ModelSettingsEdit = WithoutRequest<ModelSettingsChange>; |
| 6 | |
| 7 | export async function saveModelSettings(settings: SettingsView, change: ModelSettingsEdit): Promise<ModelSettingsResult> { |
| 8 | const expectedFingerprint = settings.modelSettingsFingerprint; |
| 9 | if (!expectedFingerprint) throw new Error("Reload Settings before saving model configuration."); |
| 10 | const requestId = crypto.randomUUID(); |
| 11 | let result: ModelSettingsResult; |
| 12 | try { |
| 13 | result = await app.ApplyModelSettings({ ...change, requestId, expectedFingerprint }); |
| 14 | } catch { |
| 15 | // Read the receipt first. Repeating an interrupted credential write could |
| 16 | // overwrite a newer edit, so this path never resubmits the operation. |
| 17 | try { result = await app.GetModelSettingsRequest(requestId); } |
| 18 | catch { throw new Error("The save result could not be confirmed. Review the current settings before saving again."); } |
| 19 | } |
| 20 | if (!result.persisted) throw new Error(result.issues?.map(issue => issue.message).join("\n") || "Model settings were not saved."); |
| 21 | return result; |
| 22 | } |
| 23 | |
| 24 | export function isModelSettingsResult(value: unknown): value is ModelSettingsResult { |
| 25 | return !!value && typeof value === "object" && typeof (value as ModelSettingsResult).persisted === "boolean"; |
| 26 | } |
| 27 |