Skip to content

Commit 1fb5d4f

Browse files
authored
fix(ui): coordinate settings mutations and visible refresh outcomes (#116940)
* fix(ui): coordinate model setup mutations * refactor(ui): return model setup completion * refactor(ui): extract model setup icon loader * fix(ui): guard queued model setup dispatch
1 parent 90ae49a commit 1fb5d4f

10 files changed

Lines changed: 647 additions & 191 deletions

‎ui/src/e2e/model-setup.e2e.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ suite.define(() => {
147147
it("completes device-code sign-in and re-detects the configured model", async () => {
148148
await suite.withPage(
149149
{
150+
...(artifactDir
151+
? { recordVideo: { dir: artifactDir, size: { width: 1280, height: 900 } } }
152+
: {}),
150153
locale: "en-US",
151154
serviceWorkers: "block",
152155
viewport: { height: 900, width: 1280 },
@@ -175,6 +178,14 @@ suite.define(() => {
175178
"wizard.next",
176179
],
177180
methodResponses: {
181+
"config.get": {
182+
config: {},
183+
sourceConfig: {},
184+
raw: "{}",
185+
hash: "config-hash-1",
186+
valid: true,
187+
issues: [],
188+
},
178189
"openclaw.setup.detect": initialDetection,
179190
"openclaw.setup.auth.start": {
180191
sessionId: "device-code-session",
@@ -202,12 +213,39 @@ suite.define(() => {
202213

203214
const response = await page.goto(`${suite.server.baseUrl}settings/model-setup`);
204215
expect(response?.status()).toBe(200);
216+
const configReadsBeforeStart = (await gateway.getRequests("config.get")).length;
217+
await gateway.deferNext("config.get");
205218
await page.getByRole("button", { name: "Pair" }).click();
206219

207220
const start = await gateway.waitForRequest("openclaw.setup.auth.start");
208221
expect(start.params).toMatchObject({ authChoice: "provider-device-code" });
222+
await expect
223+
.poll(async () => (await gateway.getRequests("config.get")).length)
224+
.toBe(configReadsBeforeStart + 1);
209225
await page.getByText("ABCD-1234").waitFor();
226+
await page.getByText("Working…").waitFor();
227+
if (artifactDir) {
228+
await mkdir(artifactDir, { recursive: true });
229+
await page.screenshot({
230+
path: path.join(artifactDir, "model-setup-refresh-pending.png"),
231+
});
232+
}
233+
await gateway.rejectDeferred("config.get", {
234+
code: "UNAVAILABLE",
235+
message: "authoritative snapshot unavailable",
236+
});
237+
await expect.poll(() => page.getByText("Working…").count()).toBe(0);
210238
await page.getByText("Expires in 14 minutes").waitFor();
239+
await page
240+
.locator("openclaw-modal-dialog")
241+
.getByRole("alert")
242+
.filter({ hasText: "authoritative snapshot unavailable" })
243+
.waitFor();
244+
if (artifactDir) {
245+
await page.screenshot({
246+
path: path.join(artifactDir, "model-setup-refresh-warning.png"),
247+
});
248+
}
211249
const signInLink = page.getByRole("link", { name: "Open sign-in page" });
212250
await expect.poll(() => signInLink.getAttribute("href")).toBe("https://example.com/device");
213251

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import type { ApplicationContext } from "../../app/context.ts";
2+
import { fetchCatalogIconBlobUrl } from "../plugins/icon-loader.ts";
3+
4+
type IconRequest = {
5+
controller: AbortController;
6+
timeout: ReturnType<typeof setTimeout>;
7+
};
8+
9+
export class ModelSetupIconLoader {
10+
private urls: Record<string, string> = {};
11+
private readonly misses = new Set<string>();
12+
private readonly requests = new Map<string, IconRequest>();
13+
14+
constructor(
15+
private readonly getContext: () => ApplicationContext,
16+
private readonly isEligible: (iconUrl: string) => boolean,
17+
private readonly onChange: (urls: Record<string, string>) => void,
18+
) {}
19+
20+
reconcile(eligible: ReadonlySet<string>): void {
21+
const nextUrls = { ...this.urls };
22+
let changed = false;
23+
for (const [iconUrl, blobUrl] of Object.entries(nextUrls)) {
24+
if (!eligible.has(iconUrl)) {
25+
URL.revokeObjectURL(blobUrl);
26+
delete nextUrls[iconUrl];
27+
changed = true;
28+
}
29+
}
30+
if (changed) {
31+
this.publish(nextUrls);
32+
}
33+
for (const [iconUrl, request] of this.requests) {
34+
if (!eligible.has(iconUrl)) {
35+
clearTimeout(request.timeout);
36+
request.controller.abort();
37+
this.requests.delete(iconUrl);
38+
}
39+
}
40+
for (const iconUrl of this.misses) {
41+
if (!eligible.has(iconUrl)) {
42+
this.misses.delete(iconUrl);
43+
}
44+
}
45+
for (const iconUrl of eligible) {
46+
if (!this.urls[iconUrl] && !this.misses.has(iconUrl) && !this.requests.has(iconUrl)) {
47+
this.fetch(iconUrl);
48+
}
49+
}
50+
}
51+
52+
invalidate(iconUrl: string): void {
53+
const request = this.requests.get(iconUrl);
54+
if (request) {
55+
clearTimeout(request.timeout);
56+
request.controller.abort();
57+
this.requests.delete(iconUrl);
58+
}
59+
const blobUrl = this.urls[iconUrl];
60+
if (blobUrl) {
61+
URL.revokeObjectURL(blobUrl);
62+
}
63+
const nextUrls = { ...this.urls };
64+
delete nextUrls[iconUrl];
65+
this.publish(nextUrls);
66+
this.misses.add(iconUrl);
67+
}
68+
69+
reset(): void {
70+
for (const request of this.requests.values()) {
71+
clearTimeout(request.timeout);
72+
request.controller.abort();
73+
}
74+
for (const blobUrl of Object.values(this.urls)) {
75+
URL.revokeObjectURL(blobUrl);
76+
}
77+
this.requests.clear();
78+
this.misses.clear();
79+
this.publish({});
80+
}
81+
82+
private fetch(iconUrl: string): void {
83+
const controller = new AbortController();
84+
const timeout = setTimeout(
85+
() => controller.abort(new DOMException("catalog icon fetch timed out", "TimeoutError")),
86+
10_000,
87+
);
88+
const request = { controller, timeout };
89+
this.requests.set(iconUrl, request);
90+
const context = this.getContext();
91+
void fetchCatalogIconBlobUrl({
92+
iconUrl,
93+
basePath: context.basePath,
94+
gatewayUrl: context.gateway.connection.gatewayUrl,
95+
auth: {
96+
hello: context.gateway.snapshot.hello,
97+
settings: { token: context.gateway.connection.token },
98+
password: context.gateway.connection.password,
99+
},
100+
signal: controller.signal,
101+
})
102+
.then((blobUrl) => {
103+
if (
104+
this.requests.get(iconUrl) !== request ||
105+
this.getContext().gateway.snapshot.phase !== "connected" ||
106+
!this.isEligible(iconUrl)
107+
) {
108+
if (blobUrl) {
109+
URL.revokeObjectURL(blobUrl);
110+
}
111+
return;
112+
}
113+
if (blobUrl) {
114+
this.publish({ ...this.urls, [iconUrl]: blobUrl });
115+
} else {
116+
this.misses.add(iconUrl);
117+
}
118+
})
119+
.catch(() => {
120+
if (this.requests.get(iconUrl) === request) {
121+
this.misses.add(iconUrl);
122+
}
123+
})
124+
.finally(() => {
125+
clearTimeout(timeout);
126+
if (this.requests.get(iconUrl) === request) {
127+
this.requests.delete(iconUrl);
128+
}
129+
});
130+
}
131+
132+
private publish(urls: Record<string, string>): void {
133+
this.urls = urls;
134+
this.onChange(urls);
135+
}
136+
}

0 commit comments

Comments
 (0)