Skip to content

Commit e79a2e1

Browse files
committed
refactor(ui): centralize UI state and error helpers
1 parent c023fe6 commit e79a2e1

21 files changed

Lines changed: 243 additions & 273 deletions

‎ui/src/api/gateway.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import {
3131
PROTOCOL_VERSION,
3232
} from "@openclaw/gateway-client/browser";
3333
// Control UI module implements gateway behavior.
34-
import { formatErrorMessage } from "@openclaw/normalization-core";
3534
import {
3635
CONTROL_UI_OWNER_BOOTSTRAP_PROFILE_HINT,
3736
type ControlUiBootstrapProfileHint,
@@ -40,7 +39,7 @@ import {
4039
BOOTSTRAP_HANDOFF_OPERATOR_SCOPES,
4140
CONTROL_UI_OWNER_BOOTSTRAP_OPERATOR_SCOPES,
4241
} from "../../../src/shared/device-bootstrap-profile.js";
43-
import { redactToolDetail } from "../lib/browser-redact.ts";
42+
import { formatUiError } from "../lib/format-error.ts";
4443
import {
4544
clearDeviceAuthToken,
4645
loadDeviceAuthToken,
@@ -225,7 +224,7 @@ function getErrorName(err: unknown): string | undefined {
225224

226225
function isBrowserWebSocketSecurityError(err: unknown): boolean {
227226
const name = getErrorName(err)?.toLowerCase();
228-
const message = formatErrorMessage(err, { redact: redactToolDetail }).toLowerCase();
227+
const message = formatUiError(err).toLowerCase();
229228
return (
230229
name === "securityerror" ||
231230
message.includes("security error") ||
@@ -236,7 +235,7 @@ function isBrowserWebSocketSecurityError(err: unknown): boolean {
236235

237236
function formatBrowserWebSocketConstructorError(err: unknown, url: string): GatewayErrorInfo {
238237
const securityError = isBrowserWebSocketSecurityError(err);
239-
const browserMessage = formatErrorMessage(err, { redact: redactToolDetail });
238+
const browserMessage = formatUiError(err);
240239
const isPlaintextWs = url.trim().toLowerCase().startsWith("ws://");
241240
const details = {
242241
code: securityError

‎ui/src/app/web-push.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Application-owned browser push subscription lifecycle.
2-
import { formatErrorMessage } from "@openclaw/normalization-core";
32
import type { GatewayBrowserClient } from "../api/gateway.ts";
4-
import { redactToolDetail } from "../lib/browser-redact.ts";
3+
import { formatUiError } from "../lib/format-error.ts";
54
import type { ApplicationGateway } from "./gateway.ts";
65

76
type WebPushSnapshot = {
@@ -89,7 +88,7 @@ export function createWebPushCapability(gateway: ApplicationGateway): WebPushCap
8988
publish({ loading: true, error: null });
9089
operation = action(client)
9190
.catch((error: unknown) => {
92-
publish({ error: formatErrorMessage(error, { redact: redactToolDetail }) });
91+
publish({ error: formatUiError(error) });
9392
})
9493
.finally(() => {
9594
operation = null;

‎ui/src/lib/format-error.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @vitest-environment node
2+
import { describe, expect, it } from "vitest";
3+
import { formatUiError } from "./format-error.ts";
4+
5+
describe("formatUiError", () => {
6+
it("formats structured causes through the browser-safe redactor", () => {
7+
const cause = Object.assign(new Error("OPENAI_API_KEY=sk-1234567890abcdef"), {
8+
code: "AUTH_FAILED",
9+
});
10+
11+
expect(formatUiError(new Error("request failed", { cause }))).toBe(
12+
"request failed | OPENAI_API_KEY=sk-123...cdef | AUTH_FAILED",
13+
);
14+
});
15+
16+
it("uses the fallback only when formatting produces an empty message", () => {
17+
expect(formatUiError("", "Request failed")).toBe("Request failed");
18+
expect(formatUiError("")).toBe("");
19+
expect(formatUiError(undefined, "Request failed")).toBe("undefined");
20+
});
21+
});

‎ui/src/lib/format-error.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { formatErrorMessage } from "@openclaw/normalization-core";
2+
import { redactToolDetail } from "./browser-redact.ts";
3+
4+
export function formatUiError(error: unknown, fallback = ""): string {
5+
return formatErrorMessage(error, { redact: redactToolDetail }) || fallback;
6+
}

‎ui/src/lib/skills/index.ts‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { formatErrorMessage } from "@openclaw/normalization-core";
21
import {
32
ClawHubTrustErrorCodes,
43
readClawHubTrustErrorDetails,
@@ -10,7 +9,7 @@ import type {
109
SkillStatusEntry,
1110
SkillStatusReport,
1211
} from "../../api/types.ts";
13-
import { redactToolDetail } from "../browser-redact.ts";
12+
import { formatUiError } from "../format-error.ts";
1413
import type { ClawHubSearchResult } from "./clawhub-search.ts";
1514
import {
1615
normalizeSkillApiKeyReplacement,
@@ -335,7 +334,7 @@ export async function loadSkills(
335334
if (!isCurrent()) {
336335
return;
337336
}
338-
state.skillsError = formatErrorMessage(err, { redact: redactToolDetail });
337+
state.skillsError = formatUiError(err);
339338
} finally {
340339
// A transient disconnect invalidates the result, not this invocation's
341340
// loading ownership. Source/scope identity still protects newer loads.
@@ -448,7 +447,7 @@ export async function loadSkillCard(state: SkillsState, skillKey: string) {
448447
if (isSkillsAgentScopeCurrent(state, agentScope)) {
449448
state.skillCardErrors = {
450449
...state.skillCardErrors,
451-
[skillKey]: formatErrorMessage(err, { redact: redactToolDetail }),
450+
[skillKey]: formatUiError(err),
452451
};
453452
}
454453
} finally {
@@ -493,7 +492,7 @@ async function loadClawHubSecurityVerdicts(state: SkillsState, report: SkillStat
493492
return;
494493
}
495494
state.clawhubVerdicts = {};
496-
state.clawhubVerdictsError = formatErrorMessage(err, { redact: redactToolDetail });
495+
state.clawhubVerdictsError = formatUiError(err);
497496
} finally {
498497
if (isSkillsAgentScopeCurrent(state, agentScope)) {
499498
state.clawhubVerdictsLoading = false;
@@ -546,7 +545,7 @@ async function runSkillMutation(
546545
) {
547546
return;
548547
}
549-
const message = formatErrorMessage(err, { redact: redactToolDetail });
548+
const message = formatUiError(err);
550549
state.skillsError = message;
551550
setSkillMessage(state, skillKey, {
552551
kind: "error",
@@ -659,7 +658,7 @@ export async function loadClawHubDetail(state: SkillsState, slug: string) {
659658
state.clawhubDetail = res ?? null;
660659
},
661660
(err) => {
662-
state.clawhubDetailError = formatErrorMessage(err, { redact: redactToolDetail });
661+
state.clawhubDetailError = formatUiError(err);
663662
},
664663
() => {
665664
state.clawhubDetailLoading = false;
@@ -725,10 +724,7 @@ export async function installFromClawHub(
725724
kind: "error",
726725
text: needsAcknowledgement
727726
? formatClawHubAcknowledgementMessage(trustDetails?.warning)
728-
: formatClawHubInstallMessage(
729-
formatErrorMessage(err, { redact: redactToolDetail }),
730-
trustDetails?.warning,
731-
),
727+
: formatClawHubInstallMessage(formatUiError(err), trustDetails?.warning),
732728
...(needsAcknowledgement ? { acknowledgeSlug: slug } : {}),
733729
...(needsAcknowledgement && trustDetails?.version
734730
? { acknowledgeVersion: trustDetails.version }

‎ui/src/pages/agents/identity-actions.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// Agent identity draft state and persistence, split out of agents-page.ts.
2-
import { formatErrorMessage } from "@openclaw/normalization-core";
32
import type { GatewayBrowserClient } from "../../api/gateway.ts";
43
import type { ApplicationContext, ApplicationNavigationPreferences } from "../../app/context.ts";
54
import { t } from "../../i18n/index.ts";
65
import { updateAgentIdentity } from "../../lib/agents/index.ts";
7-
import { redactToolDetail } from "../../lib/browser-redact.ts";
6+
import { formatUiError } from "../../lib/format-error.ts";
87
import { fileToAvatarDataUrl } from "./avatar-image.ts";
98
import type { AgentIdentityDraft } from "./panels-overview.ts";
109

@@ -104,14 +103,14 @@ export async function saveIdentityDraft(params: {
104103
await agents.refreshList();
105104
} catch (error) {
106105
refreshErrors.push(
107-
`Agent identity was saved, but the agent list refresh failed: ${formatErrorMessage(error, { redact: redactToolDetail })}`,
106+
`Agent identity was saved, but the agent list refresh failed: ${formatUiError(error)}`,
108107
);
109108
}
110109
try {
111110
await agentIdentity.ensure([agentId]);
112111
} catch (error) {
113112
refreshErrors.push(
114-
`Agent identity was saved, but the identity refresh failed: ${formatErrorMessage(error, { redact: redactToolDetail })}`,
113+
`Agent identity was saved, but the identity refresh failed: ${formatUiError(error)}`,
115114
);
116115
}
117116
if (params.isCurrent()) {

‎ui/src/pages/chat/chat-composer-capability-host.ts‎

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { formatErrorMessage } from "@openclaw/normalization-core";
21
import { asNullableRecord as asRecord } from "@openclaw/normalization-core/record-coerce";
32
import { html, nothing } from "lit";
43
import type { GatewayBrowserClient } from "../../api/gateway.ts";
@@ -18,14 +17,14 @@ import {
1817
buildToolsEffectiveRequestKey,
1918
loadToolsEffective,
2019
} from "../../lib/agents/tools-effective.ts";
21-
import { redactToolDetail } from "../../lib/browser-redact.ts";
2220
import {
2321
buildAddMcpServerPatch,
2422
MCP_SERVER_NAME_PATTERN,
2523
parseMcpTarget,
2624
patchMcpServers,
2725
summarizeMcpServers,
2826
} from "../../lib/config/mcp-servers.ts";
27+
import { formatUiError } from "../../lib/format-error.ts";
2928
import { isGatewayMethodAdvertised } from "../../lib/gateway-methods.ts";
3029
import { readSessionMethodAccess } from "../../lib/session-method-access.ts";
3130
import {
@@ -39,22 +38,8 @@ import { loadSkillStatusReport } from "../../lib/skills/index.ts";
3938
import { refreshCurrentChatSessionList } from "./chat-session.ts";
4039
import { patchChatSessionSettings } from "./chat-settings-patches.ts";
4140
import type { ChatPageHost } from "./chat-state-host.ts";
42-
import type {
43-
ChatComposerMenuSkill,
44-
ChatComposerPlusMenuProps,
45-
} from "./components/chat-composer-plus-menu.ts";
46-
47-
type CapabilityMenuProps = Omit<
48-
ChatComposerPlusMenuProps,
49-
| "attachments"
50-
| "disabled"
51-
| "open"
52-
| "view"
53-
| "toolOverrides"
54-
| "onOpenChange"
55-
| "onViewChange"
56-
| "showCapabilities"
57-
>;
41+
import type { ChatComposerMenuSkill } from "./components/chat-composer-plus-menu.ts";
42+
import type { CapabilityMenuProps } from "./components/chat-composer-types.ts";
5843

5944
type ComposerMcpServerScope = "session" | "everywhere";
6045

@@ -130,7 +115,7 @@ export class ChatComposerCapabilityHost {
130115
} catch (error) {
131116
return {
132117
ok: false,
133-
error: formatErrorMessage(error, { redact: redactToolDetail }),
118+
error: formatUiError(error),
134119
stage: "config",
135120
};
136121
}
@@ -146,7 +131,7 @@ export class ChatComposerCapabilityHost {
146131
} catch (error) {
147132
return {
148133
ok: false,
149-
error: formatErrorMessage(error, { redact: redactToolDetail }),
134+
error: formatUiError(error),
150135
stage: "session",
151136
};
152137
}
@@ -166,7 +151,7 @@ export class ChatComposerCapabilityHost {
166151
} catch (error) {
167152
return {
168153
ok: false,
169-
error: formatErrorMessage(error, { redact: redactToolDetail }),
154+
error: formatUiError(error),
170155
stage: "session",
171156
};
172157
}
@@ -410,7 +395,7 @@ export class ChatComposerCapabilityHost {
410395
} catch (error) {
411396
return {
412397
ok: false as const,
413-
error: formatErrorMessage(error, { redact: redactToolDetail }),
398+
error: formatUiError(error),
414399
};
415400
}
416401
if (!identityMatches()) {

‎ui/src/pages/chat/chat-view.ts‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ import {
4242
renderBackgroundTasksRail,
4343
type BackgroundTasksProps,
4444
} from "./components/chat-background-tasks.ts";
45-
import type { ChatComposerPlusMenuProps } from "./components/chat-composer-plus-menu.ts";
46-
import type { ChatComposerDisabledBanner } from "./components/chat-composer-types.ts";
45+
import type {
46+
CapabilityMenuProps,
47+
ChatComposerDisabledBanner,
48+
} from "./components/chat-composer-types.ts";
4749
import { isChatRunWorking, renderChatComposer } from "./components/chat-composer.ts";
4850
import { inlineChatImageFromEvent, openInlineChatImage } from "./components/chat-image-lightbox.ts";
4951
import type { ArtifactDownloadResolver } from "./components/chat-message-media.ts";
@@ -166,17 +168,7 @@ export type ChatProps = {
166168
onDismissWorkspaceConflict?: () => void;
167169
sessions: SessionsListResult | null;
168170
toolOverrides?: SessionToolOverrides;
169-
capabilityMenu?: Omit<
170-
ChatComposerPlusMenuProps,
171-
| "attachments"
172-
| "disabled"
173-
| "open"
174-
| "view"
175-
| "toolOverrides"
176-
| "onOpenChange"
177-
| "onViewChange"
178-
| "showCapabilities"
179-
>;
171+
capabilityMenu?: CapabilityMenuProps;
180172
swarmSessions?: readonly GatewaySessionRow[];
181173
/** Host context resolving global-alias session keys (scope=global fleets). */
182174
sessionHost?: UiSessionDefaultsHost | null;

‎ui/src/pages/chat/components/chat-composer-types.ts‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ import type {
2121
ChatComposerPlusMenuView,
2222
} from "./chat-composer-plus-menu.ts";
2323

24+
export type CapabilityMenuProps = Omit<
25+
ChatComposerPlusMenuProps,
26+
| "attachments"
27+
| "disabled"
28+
| "open"
29+
| "view"
30+
| "toolOverrides"
31+
| "onOpenChange"
32+
| "onViewChange"
33+
| "showCapabilities"
34+
>;
35+
2436
type ChatComposerDisabledBannerContent = {
2537
text: string;
2638
actionLabel: string;
@@ -56,17 +68,7 @@ export type ChatComposerProps = {
5668
draft: string;
5769
sessions: SessionsListResult | null;
5870
toolOverrides?: SessionToolOverrides;
59-
capabilityMenu?: Omit<
60-
ChatComposerPlusMenuProps,
61-
| "attachments"
62-
| "disabled"
63-
| "open"
64-
| "view"
65-
| "toolOverrides"
66-
| "onOpenChange"
67-
| "onViewChange"
68-
| "showCapabilities"
69-
>;
71+
capabilityMenu?: CapabilityMenuProps;
7072
providerUsage?: ProviderUsageDisplayProps;
7173
assistantName: string;
7274
sendShortcut?: ChatSendShortcut;

‎ui/src/pages/config/memory-memories.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { formatErrorMessage } from "@openclaw/normalization-core";
21
import { html, nothing, type PropertyValues } from "lit";
32
import { property, state } from "lit/decorators.js";
43
import type { AgentsWorkspaceGetResult } from "../../../../packages/gateway-protocol/src/index.js";
54
import type { MemorySearchResponse } from "../../../../src/gateway/server-methods/memory-search.ts";
65
import type { GatewayBrowserClient } from "../../api/gateway.ts";
76
import { t } from "../../i18n/index.ts";
8-
import { redactToolDetail } from "../../lib/browser-redact.ts";
7+
import { formatUiError } from "../../lib/format-error.ts";
98
import { OpenClawLightDomElement } from "../../lit/openclaw-element.ts";
109
import "../../styles/memory-memories.css";
1110

@@ -114,7 +113,7 @@ class MemoryMemoriesElement extends OpenClawLightDomElement {
114113
this.searchState = {
115114
kind: "error",
116115
query: normalizedQuery,
117-
message: formatErrorMessage(error, { redact: redactToolDetail }),
116+
message: formatUiError(error),
118117
};
119118
}
120119
}
@@ -159,7 +158,7 @@ class MemoryMemoriesElement extends OpenClawLightDomElement {
159158
}
160159
this.details = new Map(this.details).set(key, {
161160
kind: "error",
162-
message: formatErrorMessage(error, { redact: redactToolDetail }),
161+
message: formatUiError(error),
163162
});
164163
} finally {
165164
if (this.detailRequests.get(key) === request) {

0 commit comments

Comments
 (0)