Skip to content
Merged
38 changes: 30 additions & 8 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,20 @@ import {
useLazurioThreadTabs,
} from "../lazurio/LazurioEnvironmentBrowser";
import {
BuddyBackToConversation,
BuddyThreadDetailsPanel,
buddyRightPanelState,
buddyRuntimeMode,
buddyTerminalUiState,
useBuddyChatShortcuts,
useBuddyPage,
useBuddyPanelEscape,
useBuddyPanels,
useBuddyQueuedRunCount,
useBuddyWindowInView,
useBuddyHermesControls,
} from "../lazurio/BuddySurface";
import { buddyPanelOpensItself } from "../lazurio/buddySurfaceFrame";
import { BuddyPageChip } from "../lazurio/BuddySurfacePageChip";
import {
ThreadPreviewMiniPlayer,
Expand Down Expand Up @@ -604,6 +608,15 @@ export default function ChatView(props: ChatViewProps) {
// default and the chat is only the conversation; Buddy's window on the shared server is a whole
// T3 thread (GEN4).
const buddySurface = useBuddyHermesControls(environmentId);
// Lazurio overlay: Buddy's small window offers neither the right panel nor the terminal, and in his
// full-screen window the right panel opens over the whole window (BuddySurface.tsx).
// Read as plain values: the React Compiler memoizes what reads them.
const {
offered: buddyPanelsOffered,
wholeWindow: buddyWholeWindow,
framed: buddyFramed,
backLabel: buddyBackLabel,
} = useBuddyPanels();
// The page Buddy is asked from travels with the person's next message (buddySurfacePage.ts).
const buddyPage = useBuddyPage(environmentId);
const buddyInView = useBuddyWindowInView(environmentId);
Expand Down Expand Up @@ -799,7 +812,7 @@ export default function ChatView(props: ChatViewProps) {
// (BuddySurface.tsx). Read inside the selector, as the right panel's state below is.
const terminalUiState = useTerminalUiStateStore((state) =>
buddyTerminalUiState(
buddySurface,
buddySurface || !buddyPanelsOffered,
selectThreadTerminalUiState(state.terminalUiStateByThreadKey, routeThreadRef),
),
);
Expand Down Expand Up @@ -1112,8 +1125,9 @@ export default function ChatView(props: ChatViewProps) {
// changed on every render and looped as soon as the panel opened.
const rightPanelState = useRightPanelStore((state) =>
buddyRightPanelState(
buddySurface,
buddySurface || !buddyPanelsOffered,
selectThreadRightPanelState(state.byThreadKey, activeThreadRef),
buddyWholeWindow,
),
);
const shouldUsePlanSidebarSheet = prefersPlanSidebarSheet && rightPanelState.maximized !== true;
Expand All @@ -1140,7 +1154,7 @@ export default function ChatView(props: ChatViewProps) {
// A side chat has no panel of its own (upstream's `embedded`); its parent's strip serves it.
// Buddy's surface, with no panel, does not follow them either (plan DEV-6676).
useLazurioThreadTabs(buddySurface || embedded ? null : activeThreadRef, {
inlinePanel: !shouldUsePlanSidebarSheet,
inlinePanel: buddyPanelOpensItself(!shouldUsePlanSidebarSheet, buddyFramed),
draft: !isServerThread,
});
const { active: panelAnimationsActive, durationMs: panelAnimationDurationMs } =
Expand Down Expand Up @@ -2081,7 +2095,9 @@ export default function ChatView(props: ChatViewProps) {
const activeRunningTurnId = activeRuntime?.activeRunId ?? null;
useProactivePanels({
threadRef: isServerThread && !embedded ? activeThreadRef : null,
enabled: settings.proactivePanelsEnabled && !shouldUsePlanSidebarSheet && !embedded,
// Lazurio overlay: nothing opens by itself over Buddy's conversation in his window.
enabled:
settings.proactivePanelsEnabled && !shouldUsePlanSidebarSheet && !embedded && !buddyFramed,
settingsHydrated: clientSettingsHydrated,
threadDetailLoading,
runningTurnId: activeRunningTurnId,
Expand Down Expand Up @@ -2480,6 +2496,7 @@ export default function ChatView(props: ChatViewProps) {
onDiffPanelOpen,
closeRightPanelSurface,
});
useBuddyPanelEscape(buddyWholeWindow && rightPanelOpen, toggleRightPanelStable);
const pullRequestSurfaceAvailable = supportsPullRequests && pullRequestPanelTarget !== null;
const {
acknowledgeActiveThreadWoke,
Expand Down Expand Up @@ -2794,9 +2811,9 @@ export default function ChatView(props: ChatViewProps) {
}
});

// Lazurio overlay (plan DEV-6676): Buddy's surface has no terminal or right panel, so their keys
// stay the browser's there (buddySurfaceControls.ts).
useBuddyChatShortcuts(buddySurface, {
// Lazurio overlay (plan DEV-6676): Buddy's surface, and his small window, have no terminal or right
// panel, so their keys stay the browser's there (buddySurfaceControls.ts).
useBuddyChatShortcuts(buddySurface || !buddyPanelsOffered, {
enabled: activeThreadId !== null && !backgroundThread,
embedded,
composerElement: composerOverlayElement,
Expand Down Expand Up @@ -3383,6 +3400,8 @@ export default function ChatView(props: ChatViewProps) {
onToggleTerminal: toggleTerminalVisibilityStable,
onToggleThreadPanel: toggleThreadPanelStable,
onToggleRightPanel: toggleRightPanelStable,
showTerminalControl: buddyPanelsOffered,
showRightPanelControl: buddyPanelsOffered,
} satisfies PanelLayoutControlsProps;
const panelToggleControls = (
<PanelLayoutControls
Expand All @@ -3393,12 +3412,15 @@ export default function ChatView(props: ChatViewProps) {
const threadPanelHeaderControl = <ThreadPanelHeaderControl {...panelToggleControlProps} />;
const panelLayoutControls = (
<WorkspaceTitlebarControls
showMaximizeControl={!shouldUsePlanSidebarSheet}
showMaximizeControl={!shouldUsePlanSidebarSheet && !buddyWholeWindow}
rightPanelOpen={rightPanelOpen}
animationsActive={panelAnimationsActive}
maximized={rightPanelMaximized}
onToggleMaximized={toggleRightPanelMaximizedStable}
>
{buddyWholeWindow && rightPanelOpen ? (
<BuddyBackToConversation label={buddyBackLabel} onBack={toggleRightPanelStable} />
) : null}
{panelToggleControls}
</WorkspaceTitlebarControls>
);
Expand Down
86 changes: 79 additions & 7 deletions apps/web/src/lazurio/BuddySurface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@ import { pageRecordOf } from "./buddySurfacePage";
import { useLinkedUnlistedProjectIds } from "./unlistedRows";
import { BUDDY_WORKING_ROW, buddyConversationRows, buddyFailureOf } from "./buddySurfaceTimeline";
import { prefersCzech } from "./threadTabs";
import { buddyHostOfWindow, buddyWindowInView } from "./buddySurfaceFrame";
import { RIGHT_PANEL_INLINE_LAYOUT_MEDIA_QUERY } from "../rightPanelLayout";
import {
buddyEscapeTaken,
buddyHostOfWindow,
buddyPanelOverWholeWindow,
buddyPanelsOffered,
buddyWindowInView,
} from "./buddySurfaceFrame";
import { useBuddyFrame } from "./useBuddyFrame";

type BuddyConfig = { readonly lazurioBuddy?: true; readonly lazurioBuddyOrigin?: string } | null;
Expand Down Expand Up @@ -101,6 +108,64 @@ export function useBuddyWindowInView(environmentId: string): boolean {
return buddyWindowInView(framed, shown, context !== null);
}

/**
* Buddy's window's panels: `offered` the right panel and the terminal, never in his small window;
* `wholeWindow` the right panel opens over the whole window, as Matěj decided on 2026-11-21, with
* `backLabel` on its control back to the conversation. Elsewhere T3's own layout.
*/
export function useBuddyPanels() {
const framed = buddyHostOfWindow(window) !== null;
const narrow = useMediaQuery(RIGHT_PANEL_INLINE_LAYOUT_MEDIA_QUERY);
const { mode } = useBuddyFrame(framed);
const czech = useCzech(framed);
const offered = buddyPanelsOffered(framed, mode, narrow);
return {
framed,
offered,
wholeWindow: framed && offered,
backLabel: TEXT[czech ? "cs" : "en"].backToConversation,
};
}

/** In Buddy's window, the way from the right panel over the whole window back to the conversation. */
export function BuddyBackToConversation(props: {
readonly label: string;
readonly onBack: () => void;
}) {
return (
<Button
aria-label={props.label}
onClick={props.onBack}
size="icon-sm"
title={props.label}
variant="ghost"
>
<Minimize2 />
</Button>
);
}

/**
* Escape over the right panel that covers Buddy's window goes back to the conversation, before the
* frame would make the window small (buddySurfaceFrame.ts). A field or a dialog keeps its own.
*/
export function useBuddyPanelEscape(active: boolean, back: () => void) {
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- listens to the window's keys while the panel covers it.
useEffect(() => {
if (!active) return;
const onKeyDown = (event: KeyboardEvent) => {
if (event.key !== "Escape" || event.isComposing) return;
if (event.metaKey || event.ctrlKey || event.altKey || event.shiftKey) return;
const target = event.target instanceof HTMLElement ? event.target : null;
if (target?.closest("input, textarea, select, [contenteditable='true']")) return;
if (buddyEscapeTaken(document)) return;
back();
};
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, [active, back]);
}

/** Buddy's surface, read outside React when a command runs: false until the config is known. */
export const isBuddySurfaceNow = (): boolean =>
isBuddySurfaceConfig(appAtomRegistry.get(primaryServerConfigAtom));
Expand Down Expand Up @@ -133,6 +198,7 @@ function useCzech(buddy: boolean): boolean {
const TEXT = {
cs: {
fullscreen: "Na celou obrazovku",
backToConversation: "Zpět k rozhovoru",
panel: "Zmenšit do panelu",
close: "Zavřít",
days: "Dny",
Expand All @@ -153,6 +219,7 @@ const TEXT = {
},
en: {
fullscreen: "Full screen",
backToConversation: "Back to the conversation",
panel: "Back to the panel",
close: "Close",
days: "Days",
Expand Down Expand Up @@ -458,13 +525,18 @@ export const buddyTerminalUiState = (buddy: boolean, state: TerminalUiState): Te
type RightPanelState = ReturnType<typeof selectThreadRightPanelState>;

/**
* The thread's right panel as the chat reads it: in Buddy's surface closed and empty, whatever the
* thread holds (a panel opened on it earlier, a link followed into it, a page an agent showed),
* since Buddy's chat is only the conversation (root decision 0210, addendum of
* HumanAndMachines/Lazurio#648). The stored panel stays as it is. As T3's elsewhere.
* The thread's right panel as the chat reads it: closed and empty in Buddy's surface, which is only
* the conversation (root decision 0210, addendum of HumanAndMachines/Lazurio#648); maximized over
* his whole window (`wholeWindow`). The stored panel stays as it is. As T3's elsewhere.
*/
export const buddyRightPanelState = (buddy: boolean, state: RightPanelState): RightPanelState =>
buddy ? selectThreadRightPanelState({}, null) : state;
export const buddyRightPanelState = (
buddy: boolean,
state: RightPanelState,
wholeWindow = false,
): RightPanelState => {
if (buddy) return selectThreadRightPanelState({}, null);
return wholeWindow ? buddyPanelOverWholeWindow(state) : state;
};

/** The chat's shortcuts, without those Buddy's surface has no use for there. */
export function useBuddyChatShortcuts(buddy: boolean, input: ChatShortcutsInput) {
Expand Down
89 changes: 85 additions & 4 deletions apps/web/src/lazurio/buddySurfaceFrame.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { describe, expect, it } from "vite-plus/test";

import { buddyHostOfWindow, buddyWindowInView, startBuddyFrame } from "./buddySurfaceFrame";
import {
buddyHostOfWindow,
buddyPanelOpensItself,
buddyPanelOverWholeWindow,
buddyPanelsOffered,
buddyWindowInView,
startBuddyFrame,
} from "./buddySurfaceFrame";

const HOST = "https://launchpad.example-person.lazurio.io";
// What the host sends after the frame's ready, about its own Environment.
Expand All @@ -17,7 +24,13 @@ const personalContext = {
};

/** A frame's window as far as the frame uses it: its parent, its ancestors and its listeners. */
function frameWindow(options: { readonly host?: string; readonly openMenu?: boolean } = {}) {
function frameWindow(
options: {
readonly host?: string;
readonly openMenu?: boolean;
readonly panelOverWindow?: boolean;
} = {},
) {
const posted: Array<{ readonly message: unknown; readonly origin: string }> = [];
const listeners = new Map<string, (event: unknown) => void>();
const parent = {
Expand All @@ -28,7 +41,14 @@ function frameWindow(options: { readonly host?: string; readonly openMenu?: bool
location: { ancestorOrigins: [options.host ?? HOST] },
document: {
referrer: "",
querySelector: () => (options.openMenu === true ? {} : null),
// As the DOM does, it also finds the hidden dialog below.
querySelector: (selector: string) =>
selector.includes("maximized-away") ? (options.panelOverWindow === true ? {} : null) : {},
// A narrow window keeps a closed sheet mounted as a hidden dialog.
querySelectorAll: () => [
{ checkVisibility: () => false },
...(options.openMenu === true ? [{ checkVisibility: () => true }] : []),
],
},
addEventListener: (type: string, listener: (event: unknown) => void) =>
listeners.set(type, listener),
Expand Down Expand Up @@ -69,12 +89,18 @@ describe("Buddy's frame", () => {
expect(frame.layout()).toBe("panel");
});

it("leaves Escape to an open menu, a modifier or an input method", () => {
it("leaves Escape to an open menu, a panel over the window, a modifier or an input method", () => {
const menu = frameWindow({ openMenu: true });
startBuddyFrame(menu.win);
menu.key();
expect(layouts(menu.posted)).toEqual([]);

// Escape over the panel goes back to the conversation first (BuddySurface.tsx).
const panel = frameWindow({ panelOverWindow: true });
startBuddyFrame(panel.win).requestLayout("fullscreen");
panel.key();
expect(layouts(panel.posted)).toEqual(["fullscreen"]);

const { win, posted, key } = frameWindow();
startBuddyFrame(win);
key({ shiftKey: true });
Expand Down Expand Up @@ -136,6 +162,61 @@ describe("Buddy's frame", () => {
).toBe(true);
});

it("takes the size the host's window shows, also one the host chose itself, and drops a defect", () => {
const { win, posted, key, receive } = frameWindow();
const frame = startBuddyFrame(win);
expect(frame.mode()).toBeNull();
// The launcher opened the remembered full screen: one Escape leaves it.
receive({ type: "lazurio.buddy.layout", v: 1, mode: "fullscreen" });
expect(frame.mode()).toBe("fullscreen");
key();
expect(layouts(posted)).toEqual(["panel"]);
for (const defect of [
{ type: "lazurio.buddy.layout", v: 2, mode: "panel" },
{ type: "lazurio.buddy.layout", v: 1, mode: "huge" },
{ type: "lazurio.buddy.layout", v: 1, mode: "panel", extra: true },
])
receive(defect);
receive({ type: "lazurio.buddy.layout", v: 1, mode: "panel" }, "https://example.com");
receive({ type: "lazurio.buddy.layout", v: 1, mode: "panel" }, HOST, {});
expect(frame.mode()).toBe("fullscreen");
receive({ type: "lazurio.buddy.layout", v: 1, mode: "closed" });
expect(frame.mode()).toBe("closed");
expect(frame.layout()).toBe("panel");
});

it("offers the panels in the full-screen window, also a phone's, and never in the small one", () => {
const { win } = frameWindow();
const framed = buddyHostOfWindow(win) !== null;
// As the host says: full screen on a phone or a computer offers both, the small window neither.
expect(buddyPanelsOffered(framed, "fullscreen", true)).toBe(true);
expect(buddyPanelsOffered(framed, "fullscreen", false)).toBe(true);
expect(buddyPanelsOffered(framed, "panel", true)).toBe(false);
expect(buddyPanelsOffered(framed, "closed", false)).toBe(false);
// An older host never says: a frame too narrow for the panels reads as the small window.
expect(buddyPanelsOffered(framed, null, true)).toBe(false);
expect(buddyPanelsOffered(framed, null, false)).toBe(true);
// Chat on its own page, also a narrow one, keeps T3's own layout.
expect(buddyPanelsOffered(false, null, true)).toBe(true);
});

it("reads the right panel over the whole window as maximized, the same object for the same state", () => {
const stored: { isOpen: boolean; surfaces: string[]; maximized?: true } = {
isOpen: true,
surfaces: [],
};
const whole = buddyPanelOverWholeWindow(stored);
expect(whole).toEqual({ isOpen: true, surfaces: [], maximized: true });
expect(buddyPanelOverWholeWindow(stored)).toBe(whole);
expect(stored).toEqual({ isOpen: true, surfaces: [] });
const maximized = { isOpen: true, maximized: true as const };
expect(buddyPanelOverWholeWindow(maximized)).toBe(maximized);
// A page an agent reveals never opens the panel by itself in Buddy's window.
expect(buddyPanelOpensItself(true, false)).toBe(true);
expect(buddyPanelOpensItself(true, true)).toBe(false);
expect(buddyPanelOpensItself(false, false)).toBe(false);
});

it("talks to nobody outside a host's frame", () => {
const { win, posted, key } = frameWindow({ host: "https://example.com" });
const frame = startBuddyFrame(win);
Expand Down
Loading
Loading