|
| 1 | +import { t } from "../../i18n/index.ts"; |
| 2 | +import type { RuntimeConfigCapability } from "../../lib/config/index.ts"; |
| 3 | +import type { ModelProviderRowMessage } from "./view.ts"; |
| 4 | + |
| 5 | +export type ModelProviderConfigMutation = { |
| 6 | + key: string; |
| 7 | + raw: Record<string, unknown>; |
| 8 | + note: string; |
| 9 | + success: string; |
| 10 | + replacePaths?: string[]; |
| 11 | +}; |
| 12 | + |
| 13 | +export type ModelProviderConfigMutationResult = |
| 14 | + | { ok: false } |
| 15 | + | { ok: true; agentEpoch: number; warning: string | null }; |
| 16 | + |
| 17 | +type ModelProviderConfigMutationOwner = { |
| 18 | + runtimeConfig: RuntimeConfigCapability; |
| 19 | + agentEpoch: number; |
| 20 | + isCurrentClient: () => boolean; |
| 21 | + isCurrentAgent: () => boolean; |
| 22 | + refreshProviders: () => Promise<void>; |
| 23 | + setBusy: (busy: boolean) => void; |
| 24 | + setMessage: (message: ModelProviderRowMessage | null) => void; |
| 25 | +}; |
| 26 | + |
| 27 | +export function modelProviderErrorMessage(error: unknown): string { |
| 28 | + if (error instanceof Error && error.message.trim()) { |
| 29 | + return error.message; |
| 30 | + } |
| 31 | + return typeof error === "string" && error.trim() ? error : t("modelProviders.requestFailed"); |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Config patches are global; the initiating agent owns only busy/message UI. |
| 36 | + * Refresh warnings must preserve an already acknowledged mutation. |
| 37 | + */ |
| 38 | +export async function runModelProviderConfigMutation( |
| 39 | + owner: ModelProviderConfigMutationOwner, |
| 40 | + params: ModelProviderConfigMutation, |
| 41 | +): Promise<ModelProviderConfigMutationResult> { |
| 42 | + const { agentEpoch, runtimeConfig } = owner; |
| 43 | + owner.setBusy(true); |
| 44 | + owner.setMessage(null); |
| 45 | + try { |
| 46 | + await runtimeConfig.ensureLoaded(); |
| 47 | + if (!owner.isCurrentClient()) { |
| 48 | + return { ok: false }; |
| 49 | + } |
| 50 | + const patched = await runtimeConfig.patch({ |
| 51 | + raw: params.raw, |
| 52 | + note: params.note, |
| 53 | + ...(params.replacePaths ? { replacePaths: params.replacePaths } : {}), |
| 54 | + }); |
| 55 | + if (!owner.isCurrentClient()) { |
| 56 | + return { ok: false }; |
| 57 | + } |
| 58 | + if (!patched) { |
| 59 | + if (owner.isCurrentAgent()) { |
| 60 | + owner.setMessage({ |
| 61 | + kind: "error", |
| 62 | + text: runtimeConfig.state.lastError ?? t("modelProviders.configUnavailable"), |
| 63 | + }); |
| 64 | + } |
| 65 | + return { ok: false }; |
| 66 | + } |
| 67 | + |
| 68 | + let warning: string | null = null; |
| 69 | + try { |
| 70 | + await runtimeConfig.refresh(); |
| 71 | + // The config owner records ordinary config.get failures in lastError |
| 72 | + // and resolves refresh(), so rejection alone cannot detect them. |
| 73 | + warning = runtimeConfig.state.lastError; |
| 74 | + if (!warning && owner.isCurrentClient()) { |
| 75 | + await owner.refreshProviders(); |
| 76 | + } |
| 77 | + } catch (error) { |
| 78 | + // An acknowledged config patch is already committed; a later refresh |
| 79 | + // failure must not turn it into a failed credential edit. |
| 80 | + warning = modelProviderErrorMessage(error); |
| 81 | + } |
| 82 | + if (!owner.isCurrentClient()) { |
| 83 | + return { ok: false }; |
| 84 | + } |
| 85 | + if (owner.isCurrentAgent()) { |
| 86 | + owner.setMessage({ |
| 87 | + kind: "success", |
| 88 | + text: params.success, |
| 89 | + ...(warning ? { warning } : {}), |
| 90 | + }); |
| 91 | + } |
| 92 | + return { ok: true, agentEpoch, warning }; |
| 93 | + } catch (error) { |
| 94 | + if (owner.isCurrentClient() && owner.isCurrentAgent()) { |
| 95 | + owner.setMessage({ kind: "error", text: modelProviderErrorMessage(error) }); |
| 96 | + } |
| 97 | + return { ok: false }; |
| 98 | + } finally { |
| 99 | + if (owner.isCurrentClient() && owner.isCurrentAgent()) { |
| 100 | + owner.setBusy(false); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments