Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

17.5 KB
Raw Blame
<div x-data="setup()" id="studio">
  <div
    class="studio-child flex antialiased text-gray-900 bg-gray-100 dark:bg-dark dark:text-light w-full overflow-hidden max-w-[100vw] h-screen relative"
  >
    <!-- Left Sidebar -->
    <%- include('partials/studio/left-sidebar', { agentId: agentId, isSidebarOpen: 'isSidebarOpen',
    currentSidebarTab: 'currentSidebarTab', previousTab: 'previousTab', rightSidebarOpen:
    'rightSidebarOpen'}); -%>

    <div class="flex-1 flex flex-col py-3 pr-3">
      <!-- Main Content -->
      <div
        class="flex-1 h-full w-full border border-solid border-gray-300 rounded-lg relative left-[6px] md:left-[64px] md:w-[calc(100%-64px)]"
        id="builder-container"
      >
        <%- include('partials/studio/builder-menu.ejs', { agentId: agentId }); -%> <%-
        include('partials/studio/workspace'); -%>
      </div>

      <div
        id="bottom-controls-container"
        class="absolute z-2 flex flex-col w-[calc(100%-64px)] left-[64px] p-4 bottom-3 pointer-events-none"
      >
        <div
          class="overflow-hidden hidden md:block z-10 ml-auto pointer-events-auto"
          x-data="myComponent()"
          :class="mapDimensions"
        >
          <div class="group relative">
            <div
              x-show="minimapOpen"
              class="border border-solid border-gray-300 rounded-lg bg-gray-200 opacity-80"
            >
              <button
                class="absolute bottom-0 right-0 p-1 py-2 group-hover:block w-8 h-10"
                @click="toggleMinimap"
              >
                <i
                  class="opacity-0 fa-solid fa-down-left-and-up-right-to-center transition duration-75 text-gray-600 group-hover:opacity-50 hover:text-gray-950"
                ></i>
              </button>
              <canvas id="minimap" width="288" height="150"></canvas>
            </div>
            <div x-show="!minimapOpen" class="w-12 h-10">
              <button
                class="absolute bottom-0 right-0 p-1 transition duration-75 opacity-25 hover:opacity-75"
                @click="toggleMinimap"
              >
                <i class="fa-regular fa-object-group text-2xl"></i>
              </button>
            </div>
          </div>
        </div>

        <div
          id="bottom-toolbar"
          class="hidden md:flex items-center justify-between max-w-full z-10 w-full pointer-events-none"
        >
          <div class="flex items-end gap-2 pointer-events-auto -mt-8">
            <div
              class="border border-solid border-gray-200 rounded-lg overflow-hidden h-full bg-white flex flex-col items-center text-sm"
            >
              <button id="cmp-zoom-in-btn" type="button" class="hover:opacity-80 px-3 py-2 text-gray-900 hover:bg-smythos-blue-500 hover:text-white hover:border-v2-blue">
                <%- include('partials/studio/icons/zoom-in.ejs'); -%>
              </button>
              <button id="cmp-zoom-out-btn" type="button" class="hover:opacity-80 px-3 py-2 text-gray-900 hover:bg-smythos-blue-500 hover:text-white hover:border-v2-blue">
                <%- include('partials/studio/icons/zoom-out.ejs'); -%>
              </button>
            </div>
          </div>

          <div class="flex items-center gap-2 pointer-events-auto">
            <button
              id="cmp-collapse-btn"
              class="border border-solid border-gray-200 rounded-lg p-2 h-full bg-white flex gap-2 items-center text-sm text-gray-900 hover:bg-smythos-blue-500 hover:border-v2-blue hover:text-white hover:[&_path]:fill-white"
              type="button"
            >
              <%- include('partials/studio/icons/collapse.ejs'); -%>
              <span class="text text-xs">Expand</span>
            </button>
            <button
              id="cmp-prettify-btn"
              class="border border-solid border-gray-200 rounded-lg p-2 h-full bg-white flex gap-2 items-center text-sm text-gray-900 hover:bg-smythos-blue-500 hover:border-v2-blue hover:text-white hover:[&_path]:stroke-white"
              type="button"
            >
              <%- include('partials/studio/icons/prettify.ejs'); -%>
              <span class="text-xs">Prettify</span>
            </button>
            <button
              id="cmp-inspect-btn"
              class="border border-solid border-gray-200 rounded-lg p-2 h-full bg-white flex gap-2 items-center text-sm text-gray-900 stroke-[#111827] [&.active]:bg-smythos-blue-500 [&.active]:text-white [&.active]:stroke-white [&.active]:hover:bg-smyth-blue [&.active]:hover:border-smyth-blue hover:bg-smythos-blue-500 hover:border-v2-blue hover:text-white hover:[&_path]:stroke-white"
              type="button"
            >
              <%- include('partials/studio/icons/inspect.ejs'); -%>
              <span class="text-xs">Inspect</span>
            </button>
            <div id="view-only-mode" class="hidden opacity opacity-0">
              <div class="text-xs flex gap-1 items-center ml-3 h-full" style="min-width: 64px">
                <span class="text-grey-200 font-bold pr-1 pointer-events-none opacity-50"
                  >Viewer Mode</span
                >
              </div>
            </div>
          </div>
        </div>
        <div
          id="bottom-bar"
          class="z-2 hidden border border-solid border-gray-300 rounded-lg overflow-hidden pointer-events-auto mt-2"
          style="height: 400px"
        >
          <div id="debug-log-container-root" class="h-full"></div>
        </div>
      </div>
    </div>
    <div
      class="sidebar-container hidden [&.open]:flex flex-row h-[calc(100vh-82px)] transition-transform py-3 pr-3 absolute right-4 top-[66px] z-10"
      id="right-container"
    >
      <div class="h-full">
        <%- include('partials/studio/right-sidebar-emb'); -%> <%-
        include('partials/studio/right-sidebar'); -%> <%-
        include('partials/studio/right-sidebar-agent-settings'); -%>
      </div>
    </div>
  </div>

  <div class="relative">
    <div class="absolute bottom-5 right-5 flex items-center space-x-4 h-8">
      <!-- Embodiments -->
      <%- include('partials/studio/embodiments'); -%>
    </div>

    <div class="absolute top-[calc(4rem+8px)] right-5 items-end hidden md:flex">
      <div><%- include('partials/studio/debug-bar'); -%></div>
    </div>
  </div>
  <!-- Panels -->

  <!-- Settings Panel -->

  <div id="builder-deploy-modal-container" class="z-20"></div>
  <div id="builder-share-agent-modal-container" class="z-20"></div>
  <div id="self-contained-builder-modals" class="z-20"></div>
</div>

<script>
  window.SMYTHOS_EDITION = '<%= process.env.SMYTHOS_EDITION %>';
</script>

<script src="/js/alpine.min.js" defer></script>

<script>
  window.__LLM_MODELS__ = JSON.parse('<%- JSON.stringify(LLMModels) %>');
  window.isSmythStaff = JSON.parse('<%- JSON.stringify(isSmythStaff) %>');
  window.isSmythAlpha = JSON.parse('<%- JSON.stringify(isSmythAlpha) %>');
  
  // Observability flag
  window.isObservability = false;

  window.navPages = JSON.parse('<%- JSON.stringify(navPages) %>');
  window.user = JSON.parse('<%- JSON.stringify(user) %>');
  
  const setup = () => {
    const isSmythStaff = window.isSmythStaff || false;
    const isSmythAlpha = window.isSmythAlpha || false;
    const isWeaverEnabled = true; //isSmythStaff || isSmythAlpha;
    const hasAgentId = location?.pathname?.split?.('/builder/')?.[1] !== undefined;
    const agentId = location?.pathname?.split?.('/builder/')?.[1];
    const menuItems = [
      {
        name: 'Agent Weaver',
        icon: '/img/icons/weaver.svg',
        tab: 'agentBuilderTab',
        id: 'left-sidebar-agentBuilder-btn',
      },
      {
        name: 'Components',
        icon: '/img/icons/hashtag.svg',
        tab: 'buildTab',
        id: 'left-sidebar-build-btn',
      },
      {
        name: 'Agent Settings',
        icon: '/img/icons/settings.svg',
        tab: 'agentTab',
        id: 'left-sidebar-agent-btn',
      },
      {
        name: 'Global Variables',
        icon: '/img/icons/network-ai.svg',
        tab: 'variablesTab',
        id: 'left-sidebar-variables-btn',
      },
      {
        name: 'API Auth',
        icon: '/img/icons/lock.svg',
        tab: 'authTab',
        id: 'left-sidebar-auth-btn',
      },
      {
        name: 'Deploy Agent',
        icon: '/img/icons/space-rocket.svg',
        tab: 'deployTab',
        id: 'left-sidebar-deploy-btn',
      },
    ];

    if (!hasAgentId) {
      localStorage.setItem('sidebarOpen', true);
    }

    // Always use default tab (weaver or component) instead of restoring from localStorage
    const defaultTab = isWeaverEnabled ? 'agentBuilderTab' : 'buildTab';
    const currentTab = defaultTab;
    const storedMenuItem = currentTab ? menuItems.find((item) => item.tab === currentTab) : null;

    return {
      windowWidth: window.innerWidth,
      isMobile: window.innerWidth < 768,
      isSidebarOpen: localStorage.getItem('sidebarOpen') === 'true',
      rightSidebarOpen: false,
      currentSidebarTab: currentTab,
      isSettingsPanelOpen: false,
      isSubHeaderOpen: false,
      selectedOption: storedMenuItem,
      setSelectedOption(option) {
        this.selectedOption = option;
      },
      init() {
        this.$watch('currentSidebarTab', (value) => {
          const newTab = menuItems.find((item) => item.tab === value);
          if (newTab) {
            this.setSelectedOption(newTab);
          }
        });

        // Listen for the sidebarStateChanged event
        window.addEventListener('sidebarStateChanged', (event) => {
          this.isSidebarOpen = event.detail.isSidebarOpen;
          this.rightSidebarOpen = event.detail.rightSidebarOpen;
          this.currentSidebarTab = event.detail.currentSidebarTab;
        });

        window.addEventListener('resize', () => {
          this.windowWidth = window.innerWidth;
          this.isMobile = this.windowWidth < 768;
        });
      },
    };
  };
</script>
<!-- Ace config -->
<script src="/js/ace/js/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
  ace.config.set('basePath', '/js/ace/js/src-min-noconflict/');
</script>

<!-- === Dialogs === -->
<%- include('partials/dialog'); -%> <%- include('partials/dialog-secondary'); -%> <%-
include('partials/dialog-extensions'); -%>
<!-- =*= -->

<div id="builder-scripts"></div>
<%- include('partials/studio/dialog-confirm'); -%> <%- include('partials/studio/dialog-modal'); -%>
<%- include('partials/studio/dialog-new-agent'); -%> <%-
include('partials/studio/dialog-embodiment'); -%> <%-
include('partials/studio/keyboard-shortcuts-modal'); -%> <%-
include('partials/studio/feedback-modal') -%>

<div id="mobile-handler-root"></div>

<script>
  document.addEventListener('alpine:init', () => {
    Alpine.data('myComponent', () => ({
      minimapOpen: localStorage.getItem('minimapOpen') !== 'false' && !this.isMobile,

      get mapDimensions() {
        return this.minimapOpen ? 'w-72 h-40' : 'w-12 h-10';
      },

      toggleMinimap() {
        this.minimapOpen = !this.minimapOpen;
        localStorage.setItem('minimapOpen', this.minimapOpen);
      },
    }));
  });
</script>

<script>
  /**
   * @file This script manages the responsive layout of the builder UI, including the
   * sidebars and bottom controls. It uses a ResizeObserver to efficiently react to
   * size changes in the workspace and sidebars, ensuring a fluid user experience
   * without the performance overhead of traditional window resize event listeners.
   */
  document.addEventListener('DOMContentLoaded', () => {
    // --- DOM Element Selection ---
    const rightSidebar = document.getElementById('right-container');
    const bottomControls = document.getElementById('bottom-controls-container');
    const workspaceContainer = document.getElementById('workspace-container');
    const leftSidebar = document.getElementById('left-sidebar-container');
    const rightSidebars = document.querySelectorAll(
      '#right-sidebar, #embodiment-sidebar, #agent-settings-sidebar',
    );

    // --- Initial Validation ---
    if (!rightSidebar || !bottomControls || !workspaceContainer || !leftSidebar) {
      console.warn('Builder layout elements not found for dynamic resizing.');
      return;
    }

    // --- Layout Constants ---
    const MIN_CANVAS_WIDTH = 420; // Minimum width for the workspace canvas before controls are hidden.
    const MIN_SIDEBAR_WIDTH = 280; // Minimum width for the resizable sidebars.
    const SIDEBAR_NAVIGATION_WIDTH = 64; // The fixed width of the main left navigation bar.
    const GAP_WIDTH = 16; // The gap between the left sidebar and the main content.
    const MARGIN_WIDTH = 12; // A margin used in width calculations to prevent overlap.

    /**
     * @function updateBottomControlsLayout
     * @description Adjusts the width and position of the bottom controls container.
     * The container is positioned to have equal spacing from both the left sidebar and right sidebar.
     * @param {number} workspaceWidth - The current width of the workspace.
     * @param {number} rightSidebarWidth - The current width of the right sidebar.
     * @param {number} leftSidebarWidth - The current width of the left sidebar.
     * @param {boolean} isRightSidebarOpen - Whether the right sidebar is open.
     * @param {boolean} isLeftSidebarOpen - Whether the left sidebar is open.
     */
    const updateBottomControlsLayout = (
      workspaceWidth,
      rightSidebarWidth,
      leftSidebarWidth,
      isRightSidebarOpen,
      isLeftSidebarOpen,
    ) => {
      // Use viewport width to calculate the full available space
      const viewportWidth = window.innerWidth;
      
      // Calculate the left edge position: navigation bar + left sidebar (if open) + gap (if left sidebar open)
      const leftPosition = SIDEBAR_NAVIGATION_WIDTH + (isLeftSidebarOpen ? leftSidebarWidth + GAP_WIDTH : 0);
      
      // Calculate the right edge position: viewport width minus right sidebar (if open) and its right offset
      // The right sidebar container is positioned with right-4 (16px from right edge)
      const RIGHT_SIDEBAR_OFFSET = 16; // right-4 class = 1rem = 16px
      const rightEdge = viewportWidth - (isRightSidebarOpen ? rightSidebarWidth + RIGHT_SIDEBAR_OFFSET : RIGHT_SIDEBAR_OFFSET);
      
      // Calculate the container width: space between left position and right edge
      const bottomContainerWidth = rightEdge - leftPosition;

      bottomControls.style.setProperty('width', `${bottomContainerWidth}px`, 'important');
      bottomControls.style.setProperty('left', `${leftPosition}px`, 'important');
      bottomControls.style.setProperty(
        'display',
        bottomContainerWidth < MIN_CANVAS_WIDTH ? 'none' : 'flex',
        'important',
      );
    };

    /**
     * @function updateRightSidebarLayout
     * @description Manages the width of the right sidebar.
     * @param {number} workspaceWidth - The current width of the workspace.
     * @param {boolean} isRightSidebarOpen - Whether the right sidebar is open.
     * @param {boolean} isLeftSidebarOpen - Whether the left sidebar is open.
     */
    const updateRightSidebarLayout = (workspaceWidth, isRightSidebarOpen, isLeftSidebarOpen) => {
      rightSidebars.forEach((sidebar) => {
        if (
          sidebar instanceof HTMLElement &&
          !sidebar.classList.contains('hidden') &&
          isRightSidebarOpen
        ) {
          const sidebarRect = sidebar.getBoundingClientRect();
          if (sidebarRect.left <= SIDEBAR_NAVIGATION_WIDTH) {
            sidebar.style.width = `${workspaceWidth - MARGIN_WIDTH}px`;
          } else if (
            isLeftSidebarOpen &&
            sidebarRect.left <= leftSidebar.getBoundingClientRect().right + MARGIN_WIDTH
          ) {
            const newWidth = Math.max(
              sidebarRect.right - leftSidebar.getBoundingClientRect().right,
              MIN_SIDEBAR_WIDTH,
            );
            sidebar.style.width = `${newWidth}px`;
          }
          window.localStorage.setItem(`${sidebar.id}-width`, sidebar.style.width);
        }
      });
    };

    /**
     * @function updateLayout
     * @description Recalculates and applies the layout for the bottom controls and sidebars.
     * This function is the core of the responsive layout management, triggered by the ResizeObserver.
     */
    const updateLayout = () => {
      const rightSidebarWidth = rightSidebar.offsetWidth;
      const leftSidebarWidth = leftSidebar.offsetWidth;
      const workspaceWidth = workspaceContainer.offsetWidth;
      const isLeftSidebarOpen = leftSidebar.style.display !== 'none';
      const isRightSidebarOpen = rightSidebar.classList.contains('open');

      updateBottomControlsLayout(
        workspaceWidth,
        rightSidebarWidth,
        leftSidebarWidth,
        isRightSidebarOpen,
        isLeftSidebarOpen,
      );
      updateRightSidebarLayout(workspaceWidth, isRightSidebarOpen, isLeftSidebarOpen);
    };

    // --- ResizeObserver Setup ---
    // Creates a new ResizeObserver that calls the updateLayout function when observed elements resize.
    const layoutObserver = new ResizeObserver(updateLayout);

    // Observes the main layout containers for size changes.
    layoutObserver.observe(workspaceContainer);
    layoutObserver.observe(leftSidebar);
    layoutObserver.observe(rightSidebar);

    // --- Initial Layout Update ---
    // Calls updateLayout once on load to set the initial layout.
    updateLayout();

    // --- Window Resize Listener ---
    // Ensures layout updates when viewport width changes, since ResizeObserver may not catch all viewport changes
    window.addEventListener('resize', () => {
      updateLayout();
    });

    // Add cleanup on page unload
    window.addEventListener('beforeunload', () => {
      layoutObserver?.disconnect();
    });
  });
</script>