Skip to content

Commit ee0a71c

Browse files
author
Peter Steinberger
committed
fix(ui): own model provider config mutation lifecycle
1 parent 59e1d58 commit ee0a71c

6 files changed

Lines changed: 678 additions & 120 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}

‎ui/src/pages/model-providers/default-models-view.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type DefaultModelsViewProps = {
1111
mutationBlockedReason: string | null;
1212
dirty: boolean;
1313
busy: Record<string, boolean>;
14-
message?: { kind: "success" | "error"; text: string };
14+
message?: { kind: "success" | "error"; text: string; warning?: string };
1515
onPrimaryChange: (model: string) => void;
1616
onFallbackAdd: (model: string) => void;
1717
onFallbackRemove: (index: number) => void;
@@ -164,7 +164,15 @@ export function renderDefaultModels(props: DefaultModelsViewProps) {
164164
</label>
165165
</div>
166166
${props.message
167-
? html`<div class="callout ${props.message.kind}" role="status">${props.message.text}</div>`
167+
? html`<div
168+
class="callout ${props.message.kind}"
169+
role=${props.message.kind === "error" ? "alert" : "status"}
170+
>
171+
${props.message.text}
172+
</div>`
173+
: nothing}
174+
${props.message?.warning
175+
? html`<div class="callout warning" role="status">${props.message.warning}</div>`
168176
: nothing}
169177
</div>
170178
`;

0 commit comments

Comments
 (0)