Skip to content

Commit 5790957

Browse files
committed
refactor(ui): finish the assistant dock rename and drop duplicate work
The shared dock still spoke the old custodian vocabulary: .cp* classes sat beside .assistant-panel-context in one stylesheet, and --oc-custodian-reserve-* named the layout reservation for a dock that now hosts both destinations. Rename both to assistant-panel; the shipped localStorage key stays and says why. Collapse three copies of the visible-transcript refresh policy into one guard and the duplicated availability ternary into availableFor(). The shell built a full work context that the panel immediately rebuilt from two of its fields; pass the route id instead, so the snapshot is derived once by the owner that subscribes to its inputs. Make the truncation table exhaustive with satisfies so a new context field cannot reach the model unbounded.
1 parent ebc71a9 commit 5790957

6 files changed

Lines changed: 89 additions & 73 deletions

File tree

‎ui/src/app/app-shell-view.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
import { readSessionMethodAccess } from "../lib/session-method-access.ts";
2424
import { normalizeAgentId } from "../lib/sessions/session-key.ts";
2525
import { isTerminalAvailable } from "../lib/terminal-availability.ts";
26-
import { buildHomeWorkContext } from "../pages/chat/chat-work-context.ts";
2726
import type { NewSessionTarget } from "../pages/new-session/location.ts";
2827
import { pluginTabKey, pluginTabRefFromSearch } from "../pages/plugin/route.ts";
2928
import type { ShellRouteState } from "./app-host-route-state.ts";
@@ -643,7 +642,7 @@ export function renderApplicationShell(host: ShellViewHost) {
643642
.sessionPage=${activeRoute === "chat"}
644643
.pageSessionKey=${host.activeSessionKey}
645644
.pageAgentId=${selectedAgentId}
646-
.workContext=${buildHomeWorkContext(context, activeRoute, host.activeSessionKey)}
645+
.workPage=${activeRoute}
647646
.minimizeRequestId=${host.custodianMinimizeRequestId}
648647
></openclaw-assistant-panel>
649648
${isOptionalElementDefined(host.execApprovalElement)

‎ui/src/components/assistant-panel.test.ts‎

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ describe("assistant panel", () => {
7474

7575
afterEach(() => {
7676
document.body.replaceChildren();
77-
document.documentElement.style.removeProperty("--oc-custodian-reserve-bottom");
78-
document.documentElement.style.removeProperty("--oc-custodian-reserve-right");
77+
document.documentElement.style.removeProperty("--oc-assistant-reserve-bottom");
78+
document.documentElement.style.removeProperty("--oc-assistant-reserve-right");
7979
vi.unstubAllGlobals();
8080
vi.restoreAllMocks();
8181
});
@@ -168,7 +168,7 @@ describe("assistant panel", () => {
168168
await replacement.updateComplete;
169169
expect(replacement.querySelector("openclaw-home-session")).toBeNull();
170170
expect(replacement.querySelector("openclaw-custodian-surface")).not.toBeNull();
171-
expect(replacement.querySelectorAll(".cp")).toHaveLength(1);
171+
expect(replacement.querySelectorAll(".assistant-panel")).toHaveLength(1);
172172
});
173173

174174
it("minimizes a real page conversation into the dock on route leave", async () => {
@@ -185,11 +185,15 @@ describe("assistant panel", () => {
185185
await panel.updateComplete;
186186

187187
expect(panel.assistantPanelOpen).toBe(true);
188-
expect(document.documentElement.style.getPropertyValue("--oc-custodian-reserve-right")).toBe(
188+
expect(document.documentElement.style.getPropertyValue("--oc-assistant-reserve-right")).toBe(
189189
"440px",
190190
);
191191

192-
panel.querySelector<HTMLButtonElement>(".cp-actions .cp-icon:last-child")!.click();
192+
panel
193+
.querySelector<HTMLButtonElement>(
194+
".assistant-panel-actions .assistant-panel-icon:last-child",
195+
)!
196+
.click();
193197
await panel.updateComplete;
194198
expect(panel.assistantPanelOpen).toBe(false);
195199

@@ -254,13 +258,13 @@ describe("assistant panel", () => {
254258
const postMessage = vi.fn();
255259
vi.stubGlobal("webkit", { messageHandlers: { openclawWindowDrag: { postMessage } } });
256260
const cases = [
257-
[".cp-header", true],
258-
[".cp-title", true],
259-
[".cp-actions", true],
260-
[".cp-actions button:first-child", false],
261-
[".cp-actions button:first-child svg", false],
262-
[".cp-actions button:last-child", false],
263-
[".cp-actions button:last-child svg", false],
261+
[".assistant-panel-header", true],
262+
[".assistant-panel-title", true],
263+
[".assistant-panel-actions", true],
264+
[".assistant-panel-actions button:first-child", false],
265+
[".assistant-panel-actions button:first-child svg", false],
266+
[".assistant-panel-actions button:last-child", false],
267+
[".assistant-panel-actions button:last-child svg", false],
264268
["openclaw-custodian-surface", false],
265269
] as const;
266270
for (const [selector, draggable] of cases) {
@@ -281,10 +285,12 @@ describe("assistant panel", () => {
281285
expect(event.defaultPrevented, selector).toBe(draggable);
282286
}
283287

284-
panel.querySelector<HTMLButtonElement>(".cp-actions button:first-child")!.click();
288+
panel.querySelector<HTMLButtonElement>(".assistant-panel-actions button:first-child")!.click();
285289
await panel.updateComplete;
286-
expect(panel.querySelector(`.cp--${dock === "right" ? "bottom" : "right"}`)).not.toBeNull();
287-
panel.querySelector<HTMLButtonElement>(".cp-actions button:last-child")!.click();
290+
expect(
291+
panel.querySelector(`.assistant-panel--${dock === "right" ? "bottom" : "right"}`),
292+
).not.toBeNull();
293+
panel.querySelector<HTMLButtonElement>(".assistant-panel-actions button:last-child")!.click();
288294
await panel.updateComplete;
289295
expect(panel.assistantPanelOpen).toBe(false);
290296
});
@@ -362,7 +368,11 @@ describe("assistant panel", () => {
362368
await panel.updateComplete;
363369

364370
expect(
365-
(panel.querySelector(".cp-title openclaw-mascot") as HTMLElement & { mood: string }).mood,
371+
(
372+
panel.querySelector(".assistant-panel-title openclaw-mascot") as HTMLElement & {
373+
mood: string;
374+
}
375+
).mood,
366376
).toBe("thinking");
367377
});
368378
});

‎ui/src/components/assistant-panel.ts‎

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ import {
2323
} from "../lib/sessions/session-key.ts";
2424
import { OpenClawLightDomElement } from "../lit/openclaw-element.ts";
2525
import { getSafeLocalStorage } from "../local-storage.ts";
26-
import {
27-
buildHomeWorkContext,
28-
subscribeChatWorkContext,
29-
type ChatWorkContext,
30-
} from "../pages/chat/chat-work-context.ts";
26+
import { buildHomeWorkContext, subscribeChatWorkContext } from "../pages/chat/chat-work-context.ts";
3127
import {
3228
custodianSessionStore,
3329
type CustodianSessionStore,
@@ -52,6 +48,8 @@ type AssistantDestination = "home" | "custodian";
5248
type AssistantDock = Exclude<DockPanelSide, "left">;
5349

5450
const panelLayout = createDockPanelLayout({
51+
// Shipped key: operators' saved dock size and placement live here, so the
52+
// legacy custodian spelling stays even though the dock is now shared.
5553
storageKey: "openclaw.custodian.panel.v1",
5654
minHeight: 240,
5755
minWidth: 320,
@@ -71,15 +69,16 @@ export class OpenClawAssistantPanel extends OpenClawLightDomElement {
7169
@property({ type: Boolean }) sessionPage = false;
7270
@property() pageSessionKey = "";
7371
@property() pageAgentId = "";
74-
@property({ attribute: false }) workContext: ChatWorkContext | undefined;
72+
/** Route id of the page being worked on; the dock derives its own snapshot from it. */
73+
@property() workPage = "";
7574
@state() private destination: AssistantDestination = "custodian";
7675
private readonly homeLoader = new LazyCustomElementRequestController(this);
7776
@property({ type: Number }) minimizeRequestId = 0;
7877
@property({ attribute: false }) store: CustodianSessionStore = custodianSessionStore;
7978

8079
private readonly dockLayout = new DockLayoutController(this, {
8180
layout: panelLayout,
82-
reservationPrefix: "custodian",
81+
reservationPrefix: "assistant",
8382
isAvailable: () => this.available,
8483
});
8584
private readonly onToggleRequest = (event: Event) => this.handleToggleRequest(event);
@@ -99,9 +98,7 @@ export class OpenClawAssistantPanel extends OpenClawLightDomElement {
9998
window.addEventListener(CUSTODIAN_PANEL_TOGGLE_EVENT, this.onToggleRequest);
10099
window.addEventListener(HOME_PANEL_TOGGLE_EVENT, this.onToggleRequest);
101100
this.dockLayout.setSuppressed(this.suppressed);
102-
if (this.dockLayout.open && this.destination === "custodian") {
103-
void this.store.refreshTranscriptIfIdle();
104-
}
101+
this.refreshCustodianTranscript(this.dockLayout.open);
105102
}
106103

107104
override disconnectedCallback(): void {
@@ -169,9 +166,7 @@ export class OpenClawAssistantPanel extends OpenClawLightDomElement {
169166
} else {
170167
this.dockLayout.restoreOpenState();
171168
}
172-
if (!wasOpen && this.dockLayout.open && this.destination === "custodian") {
173-
void this.store.refreshTranscriptIfIdle();
174-
}
169+
this.refreshCustodianTranscript(!wasOpen && this.dockLayout.open);
175170
if (wasOpen && !this.dockLayout.open) {
176171
this.claimInput("page");
177172
}
@@ -218,8 +213,19 @@ export class OpenClawAssistantPanel extends OpenClawLightDomElement {
218213
};
219214
}
220215

216+
/** Ask OpenClaw hydrates lazily; only refresh when it actually becomes visible. */
217+
private refreshCustodianTranscript(becameVisible: boolean): void {
218+
if (becameVisible && this.destination === "custodian") {
219+
void this.store.refreshTranscriptIfIdle();
220+
}
221+
}
222+
223+
private availableFor(destination: AssistantDestination): boolean {
224+
return destination === "home" ? this.homeAvailable : this.custodianAvailable;
225+
}
226+
221227
private get available(): boolean {
222-
return this.destination === "home" ? this.homeAvailable : this.custodianAvailable;
228+
return this.availableFor(this.destination);
223229
}
224230

225231
private get suppressed(): boolean {
@@ -292,9 +298,7 @@ export class OpenClawAssistantPanel extends OpenClawLightDomElement {
292298
this.persistTarget();
293299
this.dockLayout.setOpen(open);
294300
this.claimInput(open ? "dock" : "page");
295-
if (open && this.destination === "custodian") {
296-
void this.store.refreshTranscriptIfIdle();
297-
}
301+
this.refreshCustodianTranscript(open);
298302
}
299303

300304
toggle(): void {
@@ -312,7 +316,7 @@ export class OpenClawAssistantPanel extends OpenClawLightDomElement {
312316

313317
handleToggleRequest(event: Event): void {
314318
const destination = event.type === HOME_PANEL_TOGGLE_EVENT ? "home" : "custodian";
315-
if (destination === "home" ? !this.homeAvailable : !this.custodianAvailable) {
319+
if (!this.availableFor(destination)) {
316320
return;
317321
}
318322
const detail = asNullableRecord(event instanceof CustomEvent ? event.detail : null);
@@ -342,23 +346,24 @@ export class OpenClawAssistantPanel extends OpenClawLightDomElement {
342346
const dock = this.dockLayout.dock;
343347
const home = this.homeTarget;
344348
const homeState = this.homeLoader.visibleState;
345-
const workContext =
346-
this.context && this.workContext
347-
? buildHomeWorkContext(this.context, this.workContext.page, this.workContext.sessionKey)
348-
: this.workContext;
349+
// Built here, not by the shell: the dock re-derives on its own work-context
350+
// and agent-selection subscriptions without waiting for a shell render.
351+
const workContext = this.context
352+
? buildHomeWorkContext(this.context, this.workPage, this.pageSessionKey)
353+
: undefined;
349354
const style =
350355
dock === "bottom" ? `height:${this.dockLayout.height}px` : `width:${this.dockLayout.width}px`;
351356
return html`
352357
<section
353-
class="cp cp--${dock}"
358+
class="assistant-panel assistant-panel--${dock}"
354359
style=${style}
355360
aria-label=${t("assistantPanel.title")}
356361
@pointerdown=${() => this.claimInput("dock")}
357362
@focusin=${() => this.claimInput("dock")}
358363
>
359-
${this.dockLayout.renderResizer("cp", t("assistantPanel.resize"))}
360-
<header class="rail-header cp-header" @mousedown=${beginNativeWindowDrag}>
361-
<div class="cp-title">
364+
${this.dockLayout.renderResizer("assistant-panel", t("assistantPanel.resize"))}
365+
<header class="rail-header assistant-panel-header" @mousedown=${beginNativeWindowDrag}>
366+
<div class="assistant-panel-title">
362367
<openclaw-mascot
363368
.mood=${this.destination === "custodian" && this.store.sending ? "thinking" : "idle"}
364369
.size=${16}
@@ -367,7 +372,7 @@ export class OpenClawAssistantPanel extends OpenClawLightDomElement {
367372
(destination === "home" ? this.homeAvailable : this.custodianAvailable)
368373
? html`<button
369374
type="button"
370-
class="cp-tab"
375+
class="assistant-panel-tab"
371376
aria-pressed=${this.destination === destination}
372377
@click=${() => this.openDestination(destination)}
373378
>
@@ -376,10 +381,10 @@ export class OpenClawAssistantPanel extends OpenClawLightDomElement {
376381
: nothing,
377382
)}
378383
</div>
379-
<div class="rail-header__actions cp-actions">
384+
<div class="rail-header__actions assistant-panel-actions">
380385
${this.destination === "home"
381386
? html`<button
382-
class="rail-header__action cp-icon"
387+
class="rail-header__action assistant-panel-icon"
383388
type="button"
384389
aria-label=${t("assistantPanel.openHome")}
385390
@click=${() => this.openHomePage()}
@@ -388,7 +393,7 @@ export class OpenClawAssistantPanel extends OpenClawLightDomElement {
388393
</button>`
389394
: nothing}
390395
<button
391-
class="rail-header__action cp-icon"
396+
class="rail-header__action assistant-panel-icon"
392397
type="button"
393398
aria-label=${dock === "bottom"
394399
? t("assistantPanel.dockRight")
@@ -398,7 +403,7 @@ export class OpenClawAssistantPanel extends OpenClawLightDomElement {
398403
${dock === "bottom" ? icons.panelRightOpen : icons.panelBottomOpen}
399404
</button>
400405
<button
401-
class="rail-header__action cp-icon"
406+
class="rail-header__action assistant-panel-icon"
402407
type="button"
403408
aria-label=${t("assistantPanel.close")}
404409
@click=${() => this.setOpen(false)}

‎ui/src/pages/chat/chat-work-context.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ export function buildHomeWorkContext(
9898

9999
/** A small quoted reference block, never an authorization or instruction channel. */
100100
export function formatChatWorkContext(context: ChatWorkContext): string {
101+
// Exhaustive by construction: a new ChatWorkContext field cannot reach the
102+
// model without an explicit bound here.
101103
const limits = {
102104
page: 64,
103105
title: 96,
@@ -107,10 +109,10 @@ export function formatChatWorkContext(context: ChatWorkContext): string {
107109
workspace: 224,
108110
file: 224,
109111
selection: 640,
110-
} as const;
112+
} as const satisfies Record<keyof ChatWorkContext, number>;
111113
const snapshot = Object.fromEntries(
112114
Object.entries(limits).flatMap(([key, limit]) => {
113-
// SAFETY: limits is a closed local object whose keys are all ChatWorkContext fields.
115+
// SAFETY: the satisfies clause above proves every limits key is a ChatWorkContext field.
114116
let value = truncateUtf16Safe(context[key as keyof ChatWorkContext]?.trim() ?? "", limit);
115117
// Bound the serialized form too: quotes/control characters can expand sixfold.
116118
while (JSON.stringify(value).length > limit) {

0 commit comments

Comments
 (0)