Skip to content

Agent Zero AI framework

7.9 KB
Raw Blame
import { createStore } from "/js/AlpineStore.js";
import { ttsService } from "/js/tts-service.js";
import { applyModeSteps } from "/components/messages/process-group/process-group-dom.js";

const UI_VISIBILITY_DEFAULTS = {
  projectSelector: { mobile: true, desktop: true },
  time: { mobile: false, desktop: true },
  connectionStatus: { mobile: true, desktop: true },
  rightCanvasRail: { mobile: true, desktop: true },
};

function normalizeUiVisibility(value = {}) {
  return Object.fromEntries(
    Object.entries({
      ...Object.fromEntries(Object.keys(value || {}).filter((id) => id.startsWith("canvas:") && id.length > 7)
        .map((id) => [id, { mobile: true, desktop: true }])),
      ...UI_VISIBILITY_DEFAULTS,
    }).map(([control, defaults]) => [
      control,
      {
        mobile: typeof value?.[control]?.mobile === "boolean" ? value[control].mobile : defaults.mobile,
        desktop: typeof value?.[control]?.desktop === "boolean" ? value[control].desktop : defaults.desktop,
      },
    ])
  );
}

// Preferences store centralizes user preference toggles and side-effects
const model = {
  _initialized: false,

  // UI toggles (initialized with safe defaults, loaded from localStorage in init)
  get autoScroll() {
    return this._autoScroll;
  },
  set autoScroll(value) {
    this._autoScroll = value;
    this._applyAutoScroll(value);
  },
  _autoScroll: true,

  get darkMode() {
    return this._darkMode;
  },
  set darkMode(value) {
    this._darkMode = value;
    this._applyDarkMode(value);
  },
  _darkMode: true,

  get speech() {
    return this._speech;
  },
  set speech(value) {
    this._speech = value;
    this._applySpeech(value);
  },
  _speech: false,

  get showUtils() {
    return this._showUtils;
  },
  set showUtils(value) {
    this._showUtils = value;
    this._applyShowUtils(value);
  },
  _showUtils: false,

  get showToolArgs() {
    return this._showToolArgs;
  },
  set showToolArgs(value) {
    this._showToolArgs = value;
    this._applyShowToolArgs(value);
  },
  _showToolArgs: false,

  // Chat container width preference for HiDPI/large screens
  get chatWidth() {
    return this._chatWidth;
  },
  set chatWidth(value) {
    this._chatWidth = value;
    this._applyChatWidth(value);
  },
  _chatWidth: "55", // Default width in em (standard)

  // Width presets: { label, value in em }
  chatWidthOptions: [
    { label: "MIN", value: "40" },
    { label: "WIDE", value: "55" },
    { label: "2X", value: "80" },
    { label: "FULL", value: "full" },
  ],

  // Detail mode for process groups/steps expansion
  get detailMode() {
    return this._detailMode;
  },
  set detailMode(value) {
    this._detailMode = value;
    this._applyDetailMode(value);
  },
  _detailMode: "current", // Default: show current step only

  _uiVisibility: normalizeUiVisibility(globalThis.runtimeInfo?.uiControlVisibility),
  _isMobileViewport: false,

  registerUiControlVisibility(control, defaults = {}) {
    const id = String(control || "").trim();
    if (!id) return;
    UI_VISIBILITY_DEFAULTS[id] = {
      mobile: defaults.mobile !== false,
      desktop: defaults.desktop !== false,
    };
    this._uiVisibility = normalizeUiVisibility({
      ...(globalThis.runtimeInfo?.uiControlVisibility || {}),
      ...(this._uiVisibility || {}),
    });
  },

  uiVisibilitySnapshot() {
    return normalizeUiVisibility(this._uiVisibility);
  },

  setUiVisibility(value) {
    this._uiVisibility = normalizeUiVisibility(value);
  },

  isUiControlVisible(control) {
    const device = this._isMobileViewport ? "mobile" : "desktop";
    return this._uiVisibility?.[control]?.[device] !== false;
  },

  // Detail mode options for UI sidebar
  detailModeOptions: [
    { label: "NO", value: "collapsed", title: "All collapsed" },
    { label: "LIST", value: "list", title: "Steps collapsed" },
    { label: "STEP", value: "current", title: "Current step only" },
    { label: "ALL", value: "expanded", title: "All expanded" },
  ],

  // Initialize preferences and apply current state
  init() {
    if (this._initialized) return;
    this._initialized = true;

    try {
      // Load persisted preferences with safe fallbacks
      try {
        const storedDarkMode = localStorage.getItem("darkMode");
        this._darkMode = storedDarkMode !== "false";
      } catch {
        this._darkMode = true; // Default to dark mode if localStorage is unavailable
      }

      try {
        const storedSpeech = localStorage.getItem("speech");
        this._speech = storedSpeech === "true";
      } catch {
        this._speech = false; // Default to speech off if localStorage is unavailable
      }

      // Load chat width preference
      try {
        const storedChatWidth = localStorage.getItem("chatWidth");
        if (storedChatWidth && this.chatWidthOptions.some(opt => opt.value === storedChatWidth)) {
          this._chatWidth = storedChatWidth;
        }
      } catch {
        this._chatWidth = "55"; // Default to standard
      }

      // Load detail mode preference
      try {
        const storedDetailMode = localStorage.getItem("detailMode");
        if (storedDetailMode && this.detailModeOptions.some(opt => opt.value === storedDetailMode)) {
          this._detailMode = storedDetailMode;
        }
      } catch {
        this._detailMode = "current"; // Default
      }

      // load utility messages preference
      try{
        const storedShowUtils = localStorage.getItem("showUtils");
        this._showUtils = storedShowUtils === "true";
      } catch {
        this._showUtils = false; // Default to speech off if localStorage is unavailable
      }

      // load tool arguments display preference
      try {
        const storedShowToolArgs = localStorage.getItem("showToolArgs");
        this._showToolArgs = storedShowToolArgs === "true";
      } catch {
        this._showToolArgs = false; // Default to hidden if localStorage is unavailable
      }

      this._isMobileViewport = globalThis.innerWidth <= 768;
      globalThis.addEventListener("resize", () => {
        this._isMobileViewport = globalThis.innerWidth <= 768;
      });

      // Apply all preferences
      this._applyDarkMode(this._darkMode);
      this._applyAutoScroll(this._autoScroll);
      this._applySpeech(this._speech);
      this._applyShowUtils(this._showUtils);
      this._applyShowToolArgs(this._showToolArgs);
      this._applyChatWidth(this._chatWidth);
      this._applyDetailMode(this._detailMode);
    } catch (e) {
      console.error("Failed to initialize preferences store", e);
    }
  },

  _applyAutoScroll(value) {
    // nothing for now
  },

  _applyDarkMode(value) {
    if (value) {
      document.body.classList.remove("light-mode");
      document.body.classList.add("dark-mode");
    } else {
      document.body.classList.remove("dark-mode");
      document.body.classList.add("light-mode");
    }
    localStorage.setItem("darkMode", value);
  },

  _applySpeech(value) {
    localStorage.setItem("speech", value);
    if (!value) ttsService.stop();
  },


  _applyShowUtils(value) {
    localStorage.setItem("showUtils", value);
    document.documentElement.classList.toggle(
      "show-utility-messages",
      Boolean(value),
    );
  },

  _applyShowToolArgs(value) {
    localStorage.setItem("showToolArgs", value);
  },

  _applyChatWidth(value) {
    localStorage.setItem("chatWidth", value);
    // Set CSS custom property for chat max-width
    const root = document.documentElement;
    if (value === "full") {
      root.style.setProperty("--chat-max-width", "100%");
    } else {
      root.style.setProperty("--chat-max-width", `${value}em`);
    }
  },

  applyCurrentDetailMode(chatHistory = undefined) {
    return applyModeSteps(this._detailMode, this._showUtils, chatHistory);
  },

  _applyDetailMode(value) {
    localStorage.setItem("detailMode", value);
    // Apply mode to all existing DOM elements
    void this.applyCurrentDetailMode().catch((error) => {
      console.error("Failed to apply process detail mode", error);
    });
  },
};

export const store = createStore("preferences", model);