Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

8.6 KB
Raw Blame
'use client';

import { lazy, Suspense, useRef } from 'react';

import { useAui } from '../assistant-ui.js';
import { DesktopOnlyNotice } from '../atoms/DesktopOnlyNotice.js';
import { NamedAgentHeaderLabel } from '../atoms/NamedAgentHeaderLabel.js';
import { PageHeader } from '../atoms/PageHeader.js';
import { ShellActions } from '../atoms/ShellActions.js';
import { auiButtonClass } from '../atoms/lib/buttonClasses.js';
import { cn } from '../atoms/lib/cn.js';
import { useIsMobile } from '../atoms/lib/useIsMobile.js';
import { Spinner } from '../atoms/primitives/Spinner.js';
import { AgentConfigDrawerContainer } from '../containers/AgentConfigDrawerContainer.js';
import { Thread } from '../containers/Thread.js';
import { useChatChromeActionsVisible } from '../hooks/useChatChromeActionsVisible.js';
import { Icon } from '../icons/Icon.js';
import { shellIsCreateAgent, useOptionalShellMode } from '../server/ShellModeContext.js';
import { useSlot } from '../theme/SlotsProvider.js';

const TruefoundrySettingsBuilder = lazy(() => import('../containers/SettingsBuilder/index.js'));
const SchedulesPage = lazy(() =>
  import('../atoms/schedules/SchedulesPage.js').then(m => ({ default: m.SchedulesPage })),
);
const EnvironmentsPage = lazy(() =>
  import('../atoms/environments/EnvironmentsPage.js').then(m => ({ default: m.EnvironmentsPage })),
);

export function DrawerLayout({ className }: { className?: string }) {
  const aui = useAui();
  const shell = useOptionalShellMode();
  const isMobile = useIsMobile();
  const ClearChatButton = useSlot('ClearChatButton');
  const ResetAgentBuilderButton = useSlot('ResetAgentBuilderButton');
  const AgentDetailsPage = useSlot('AgentDetailsPage');
  const AgentsLibrary = useSlot('AgentsLibrary');
  const SessionsPage = useSlot('SessionsPage');
  const ShareChatButton = useSlot('ShareChatButton');
  const SaveAgentButton = useSlot('SaveAgentButton');
  const SelectAgentEmptyState = useSlot('SelectAgentEmptyState');
  const UserAvatar = useSlot('UserAvatar');
  const mainRef = useRef<HTMLDivElement>(null);
  const isIdle = shell?.mode.status === 'idle';
  const settingsOpen = shell?.settingsOpen === true;
  const libraryOpen = shell?.libraryOpen === true;
  const sessionsOpen = shell?.sessionsOpen === true;
  const schedulesOpen = shell?.schedulesOpen === true;
  const environmentsOpen = shell?.environmentsOpen === true;
  const overlayOpen = settingsOpen || libraryOpen || sessionsOpen || schedulesOpen || environmentsOpen;
  const chatChromeActionsVisible = useChatChromeActionsVisible();
  const isCreateAgent = shell != null && shellIsCreateAgent(shell.mode);
  const showDesktopOnlyNotice =
    isMobile && (sessionsOpen || schedulesOpen || environmentsOpen || (isCreateAgent && !settingsOpen && !libraryOpen));
  const showAgentConfig = isCreateAgent && !overlayOpen && !isMobile;
  const showNewActions = shell?.isNewChatEnabled !== false;

  const handleNewChat = () => {
    shell?.setLibraryOpen(false);
    shell?.setSessionsOpen(false);
    if (shell?.isComposerEnabled) {
      shell.openDraft();
      return;
    }
    shell?.setSettingsOpen(false);
    shell?.setSchedulesOpen(false);
    shell?.setEnvironmentsOpen(false);
    void Promise.resolve(aui.threads().switchToNewThread()).catch(() => undefined);
  };

  const handleNewAgent = () => {
    shell?.setLibraryOpen(false);
    shell?.setSessionsOpen(false);
    if (shell?.isComposerEnabled) {
      shell.openAgentBuilder();
    }
  };

  const handleBackToChat = () => {
    shell?.setLibraryOpen(false);
    shell?.setSchedulesOpen(false);
    shell?.setSessionsOpen(false);
    shell?.setEnvironmentsOpen(false);
    // Only exit create-agent when leaving the mobile Build Agent notice — not when
    // dismissing Agents/Schedules over an in-progress desktop builder.
    if (showDesktopOnlyNotice && isCreateAgent) shell?.openDraft();
  };

  const showBackToChat = showDesktopOnlyNotice || libraryOpen || schedulesOpen || environmentsOpen;

  return (
    <div className={cn('relative flex h-full min-h-0 w-full bg-primary-bg', className)}>
      {showAgentConfig ? (
        <aside
          role="dialog"
          aria-label="Agent Config"
          className="absolute inset-y-0 left-0 z-20 w-full max-w-sm border-r border-border shadow-xl md:static md:z-auto md:w-88 md:max-w-none md:shrink-0 md:shadow-none"
        >
          <AgentConfigDrawerContainer />
        </aside>
      ) : null}

      <div className="flex min-h-0 min-w-0 flex-1 flex-col">
        {/* Keep ShellActions mounted across Settings open/close so host action-slot state persists. */}
        <PageHeader
          className="bg-topbar-bg"
          title={
            showBackToChat ? (
              <button
                type="button"
                className={auiButtonClass({ variant: 'ghost', size: 'small' })}
                onClick={handleBackToChat}
              >
                <Icon name="arrow-left" />
                Back to chat
              </button>
            ) : !overlayOpen ? (
              <NamedAgentHeaderLabel />
            ) : null
          }
          end={
            <>
              {!overlayOpen && !showDesktopOnlyNotice ? (
                <>
                  <ResetAgentBuilderButton />
                  <ShareChatButton />
                  <ClearChatButton />
                  <SaveAgentButton />
                </>
              ) : null}
              <ShellActions key="shell-actions" />
              <UserAvatar />
              {!overlayOpen && !showDesktopOnlyNotice ? (
                <>
                  {showNewActions && !chatChromeActionsVisible ? (
                    <button
                      type="button"
                      aria-label="New Chat"
                      title="New Chat"
                      className={auiButtonClass({ variant: 'ghost', size: 'icon' })}
                      onClick={handleNewChat}
                    >
                      <Icon name="square-pen" />
                    </button>
                  ) : null}
                  {showNewActions && shell?.isComposerEnabled && !isMobile ? (
                    <button
                      type="button"
                      aria-label="New Agent"
                      title="New Agent"
                      className={auiButtonClass({ variant: 'ghost', size: 'icon' })}
                      onClick={handleNewAgent}
                    >
                      <Icon name="agent-2" />
                    </button>
                  ) : null}
                </>
              ) : null}
            </>
          }
        />
        <div ref={mainRef} className="min-h-0 min-w-0 flex-1">
          {settingsOpen ? (
            <Suspense
              fallback={
                <div
                  className="flex h-full items-center justify-center"
                  role="status"
                  aria-live="polite"
                  aria-busy="true"
                >
                  <Spinner size={28} className="text-text-primary" />
                  <span className="sr-only">Loading</span>
                </div>
              }
            >
              <TruefoundrySettingsBuilder />
            </Suspense>
          ) : showDesktopOnlyNotice ? (
            <DesktopOnlyNotice />
          ) : sessionsOpen ? (
            <SessionsPage />
          ) : libraryOpen && shell?.libraryAgentId != null ? (
            <AgentDetailsPage key={shell.libraryAgentId} agentId={shell.libraryAgentId} />
          ) : libraryOpen ? (
            <AgentsLibrary />
          ) : schedulesOpen ? (
            <Suspense
              fallback={
                <div
                  className="flex h-full items-center justify-center"
                  role="status"
                  aria-live="polite"
                  aria-busy="true"
                >
                  <Spinner size={28} className="text-text-primary" />
                  <span className="sr-only">Loading</span>
                </div>
              }
            >
              <SchedulesPage />
            </Suspense>
          ) : environmentsOpen ? (
            <Suspense
              fallback={
                <div
                  className="flex h-full items-center justify-center"
                  role="status"
                  aria-live="polite"
                  aria-busy="true"
                >
                  <Spinner size={28} className="text-text-primary" />
                  <span className="sr-only">Loading</span>
                </div>
              }
            >
              <EnvironmentsPage />
            </Suspense>
          ) : isIdle ? (
            <SelectAgentEmptyState />
          ) : (
            <Thread />
          )}
        </div>
      </div>
    </div>
  );
}