Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

15.4 KB
Raw Blame
import type { HeteroPermissionMode, WorkspaceUserPreference } from '@lobechat/types';
import {
  applyHeteroPermissionModeDir,
  applyHeteroPlanModeDir,
  applyWorktreeModeDir,
} from '@lobechat/types';
import { mergeNotificationSettings } from '@lobechat/utils/mergeNotificationSettings';
import isEqual from 'fast-deep-equal';
import type { ReplicaSlice } from 'local-replica/zustand';
import { useSyncExternalStore } from 'react';

import {
  getActiveWorkspaceId,
  useActiveWorkspaceId,
} from '@/business/client/hooks/useActiveWorkspaceId';
import { createReplicaSlice, type ReplicaLens, revalidateReplica } from '@/libs/replica';
import { userService } from '@/services/user';
import { workspaceUserSettingsService } from '@/services/workspaceUserSettings';
import { refreshAgentDeviceBindings } from '@/store/device/agentDeviceBinding';
import { type StoreSetter } from '@/store/types';
import { setNamespace } from '@/utils/storeDebug';

import type { UserState } from '../../initialState';
import type { UserStore } from '../../store';
import {
  getWorkspaceUserPreferenceResource,
  type WorkspaceUserPreferenceParams,
} from './projection';

const n = setNamespace('workspaceUserSettings');

/** Per-directory habits stored the same way in both preference buckets. */
type DirectoryPreference = Pick<
  WorkspaceUserPreference,
  'heteroPermissionModeDirs' | 'heteroPlanModeDirs' | 'worktreeModeDirs'
>;

type Setter = StoreSetter<UserStore>;

type PreferenceSlice = ReplicaSlice<
  UserStore,
  WorkspaceUserPreferenceParams,
  WorkspaceUserPreference,
  WorkspaceUserPreference | null
>;

/**
 * Read side of the store api this slice needs to observe its own view. It reads
 * through the store instead of importing `useUserStore`: this slice is part of
 * that store, so a value import closes the `store β†’ slice β†’ store` module cycle
 * the circular lint rejects (see `useFetchWorkspaceUserPreference`).
 */
interface PreferenceStoreApi {
  getState: () => UserState;
  subscribe: (listener: () => void) => () => void;
}

/** Never notifies β€” what the snapshot falls back to when no store api is wired. */
const EMPTY_SUBSCRIBE = () => () => {};

/**
 * Fold a preference patch onto the current bucket, mirroring the server's deep
 * merge (`WorkspaceUserSettingsModel`) so an optimistic write never drops a
 * sibling override / notification toggle the DB keeps.
 */
const mergePreference = (
  previous: WorkspaceUserPreference,
  patch: Partial<WorkspaceUserPreference>,
): WorkspaceUserPreference => ({
  ...previous,
  ...patch,
  ...(patch.agentDeviceOverrides
    ? { agentDeviceOverrides: { ...previous.agentDeviceOverrides, ...patch.agentDeviceOverrides } }
    : {}),
  ...(patch.agentModelOverrides
    ? { agentModelOverrides: { ...previous.agentModelOverrides, ...patch.agentModelOverrides } }
    : {}),
  ...(patch.agentModeOverrides
    ? { agentModeOverrides: { ...previous.agentModeOverrides, ...patch.agentModeOverrides } }
    : {}),
  ...(patch.notification
    ? { notification: mergeNotificationSettings(previous.notification, patch.notification) }
    : {}),
  ...(patch.sidebarAgentVisibilityOverrides
    ? {
        sidebarAgentVisibilityOverrides: {
          ...previous.sidebarAgentVisibilityOverrides,
          ...patch.sidebarAgentVisibilityOverrides,
        },
      }
    : {}),
});

/**
 * Single-slot view of the workspace-user preference replica: the active
 * workspace's bucket stays on the long-standing fields
 * (`workspaceUserPreference` + `workspaceUserPreferenceWorkspaceId`), so every
 * existing selector / consumer keeps reading the same place. Only the entry of
 * the active workspace is materialized β€” the marker gates that.
 */
const workspaceUserPreferenceLens: ReplicaLens<UserStore, WorkspaceUserPreference> = {
  clear: () => ({ workspaceUserPreference: {}, workspaceUserPreferenceWorkspaceId: null }),
  get: (state, key) =>
    state.workspaceUserPreferenceWorkspaceId === key ? state.workspaceUserPreference : undefined,
  keys: (state) =>
    state.workspaceUserPreferenceWorkspaceId === null
      ? []
      : [state.workspaceUserPreferenceWorkspaceId],
  set: (_state, key, data) =>
    data === undefined
      ? { workspaceUserPreference: {}, workspaceUserPreferenceWorkspaceId: null }
      : { workspaceUserPreference: data, workspaceUserPreferenceWorkspaceId: key },
};

/**
 * Slice for the caller's `workspace_user_settings.preference` bucket. Reads go
 * through the replica (local-first, idempotent, scope-partitioned): the
 * identity partition `${userId}:${workspaceId}` owns the workspace boundary, so
 * switching workspaces drops the previous bucket before paint and reads the
 * right one. Personal mode short-circuits without hitting the network β€” the row
 * doesn't exist there and the server would reject the request.
 *
 * Writes merge the patch into the view optimistically (so a picker observes its
 * pick immediately) and roll the view back if the mutation fails.
 */
export const createWorkspaceUserSettingsSlice = (
  set: Setter,
  get: () => UserStore,
  _api?: PreferenceStoreApi,
) => new WorkspaceUserSettingsActionImpl(set, get, _api);

export class WorkspaceUserSettingsActionImpl {
  readonly #api: PreferenceStoreApi | undefined;
  readonly #get: () => UserStore;
  readonly #set: Setter;
  /**
   * Local-first replica of the active workspace's preference bucket (its view
   * is `workspaceUserPreference`), created on first use rather than in the
   * constructor: `@/libs/replica` reaches `@/store/user` through its
   * cache-scope wiring, and the constructor runs while the user store module is
   * still initializing β€” evaluating the binding there is a module cycle (see
   * `projection.ts`).
   */
  #preference?: PreferenceSlice;

  constructor(set: Setter, get: () => UserStore, _api?: PreferenceStoreApi) {
    this.#api = _api;
    this.#set = set;
    this.#get = get;
  }

  #replica(): PreferenceSlice {
    this.#preference ??= createReplicaSlice(getWorkspaceUserPreferenceResource(), {
      actionPrefix: n('preference'),
      fetcher: () => workspaceUserSettingsService.getPreference(),
      get: this.#get,
      // A `null` response (no server row) reads as the empty bucket; an
      // unchanged response must not re-render every consumer.
      merge: (incoming, confirmed) => {
        const next = incoming ?? {};
        return isEqual(next, confirmed) ? undefined : next;
      },
      set: this.#set,
      stateKey: 'workspaceUserPreferenceReplica',
      view: workspaceUserPreferenceLens,
    });
    return this.#preference;
  }

  /**
   * Sync the active workspace's preference into the store view. Several
   * surfaces mount this (sidebar, chat input, view-all page); the replica
   * dedupes the underlying read by scope + key, so calling it from each is
   * safe. Returns the shape the existing consumers expect
   * (`{ data, isLoading, mutate }`), where `data === undefined` means "not
   * loaded yet β€” fall back to the store bucket".
   */
  useFetchWorkspaceUserPreference = () => {
    const workspaceId = useActiveWorkspaceId();
    const sync = this.#replica().useSync(workspaceId ? { workspaceId } : null, {
      enabled: !!workspaceId,
    });

    // Read the view through the store api rather than `useUserStore`: importing
    // the store hook here would close a `store β†’ slice β†’ store` module cycle
    // (`dpdm` rejects it). Same reactive read, no module edge β€” the shape
    // mirrors `useChatInputResourceAccess`.
    const api = this.#api;
    const data = useSyncExternalStore(
      api ? api.subscribe : EMPTY_SUBSCRIBE,
      () => {
        const state = api ? api.getState() : this.#get();
        return workspaceId && state.workspaceUserPreferenceWorkspaceId === workspaceId
          ? state.workspaceUserPreference
          : undefined;
      },
      () => undefined,
    );

    return {
      data,
      // Mirrors the old SWR `isLoading`: no value for this workspace yet and
      // the local copy hasn't settled (or the network is still confirming it).
      isLoading: !!workspaceId && data === undefined && (!sync.isHydrated || sync.isValidating),
      mutate: sync.revalidate,
    };
  };

  /**
   * Re-read the active workspace's preference from the server. Call it after
   * a server-side write to this member's preference that the client did not
   * make itself (e.g. publishing a pinned private Agent moves its pin into
   * `sidebarPinnedItemIds`), so the next local whole-array write is not based
   * on a stale copy.
   */
  refreshWorkspaceUserPreference = async (): Promise<void> => {
    const workspaceId = getActiveWorkspaceId();
    if (!workspaceId) return;
    await revalidateReplica(getWorkspaceUserPreferenceResource(), workspaceId);
  };

  updateWorkspaceUserPreference = async (
    patch: Partial<WorkspaceUserPreference>,
  ): Promise<void> => {
    const workspaceId = getActiveWorkspaceId();
    const previous = this.#get().workspaceUserPreference;

    // Personal mode (no active workspace) has no row to key on; keep the server
    // call so the pre-migration contract is unchanged.
    if (!workspaceId) {
      await workspaceUserSettingsService.updatePreference(patch);
      return;
    }

    const replica = this.#replica();

    // A picker can fire before the local copy was read. Read it first: seeding
    // an entry now would make the persisted bucket unhydratable (hydration only
    // fills an empty slot), so a failed write would hide the cached overrides
    // and a successful one would persist the bare patch over them. Only a write
    // that finds nothing loaded pays for the (local, IndexedDB) read.
    if (replica.getConfirmed(workspaceId) === undefined) {
      await replica.hydrate({ workspaceId });

      // The active workspace can change while the (local, IndexedDB) read is
      // pending. Everything after this point β€” the local seed/overlay and, above
      // all, the service call, which carries the *current* workspace context β€”
      // would then apply a preference picked in the previous workspace to the
      // new one. Abandon the write: the pick belonged to the workspace that was
      // active when it was made.
      if (getActiveWorkspaceId() !== workspaceId) return;
    }

    // Seed the active entry when there is still nothing loaded (no local copy),
    // so the optimistic overlay below always has a value to apply onto.
    // `persist: false` β€” this is not a server-confirmed value.
    replica.update(workspaceId, (data) => data ?? previous, { persist: false });

    // Optimistic merge: the picker's own re-render sees the new choice on the
    // next frame, and a failed write rolls the view back to the pre-write
    // bucket. Revalidate once the write settles: it supersedes a read that
    // started before the write (the driver discards that response), which
    // would otherwise land after the overlay is gone and restore the old value
    // in the view and IndexedDB.
    await replica.optimistic(
      workspaceId,
      (data) => mergePreference(data, patch),
      () => workspaceUserSettingsService.updatePreference(patch),
      { revalidate: true },
    );
    // The caller's per-agent device override changes where an agent routes for
    // them, which the sidebar presence dots cache per agent. (Personal mode
    // has no overrides, so the early return above needs no refresh.)
    if (patch.agentDeviceOverrides) void refreshAgentDeviceBindings();
  };

  /**
   * Turn the composer's "New worktree" switch on or off for one directory
   * (`getWorktreeModeKey`). See `#setDirectoryPreference` for where it lives.
   */
  setWorktreeModeDir = async (key: string, enabled: boolean): Promise<void> =>
    this.#setDirectoryPreference({
      apply: (preference) => ({
        worktreeModeDirs: applyWorktreeModeDir(preference.worktreeModeDirs, key, enabled),
      }),
      name: 'setWorktreeModeDir',
      writePersonal: async () => ({
        worktreeModeDirs: await userService.setWorktreeModeDir(key, enabled),
      }),
      writeWorkspace: () => workspaceUserSettingsService.setWorktreeModeDir(key, enabled),
    });

  /**
   * Remember whether the Codex Plan switch was left on for one directory (same
   * key as the worktree switch), which a new Codex topic there starts with.
   */
  setHeteroPlanModeDir = async (key: string, enabled: boolean): Promise<void> =>
    this.#setDirectoryPreference({
      apply: (preference) => ({
        heteroPlanModeDirs: applyHeteroPlanModeDir(preference.heteroPlanModeDirs, key, enabled),
      }),
      name: 'setHeteroPlanModeDir',
      writePersonal: async () => ({
        heteroPlanModeDirs: await userService.setHeteroPlanModeDir(key, enabled),
      }),
      writeWorkspace: () => workspaceUserSettingsService.setHeteroPlanModeDir(key, enabled),
    });

  /**
   * Remember the Claude Code permission mode picked for one directory (same key
   * as the worktree switch), which a new topic there starts in.
   */
  setHeteroPermissionModeDir = async (key: string, mode: HeteroPermissionMode): Promise<void> =>
    this.#setDirectoryPreference({
      apply: (preference) => ({
        heteroPermissionModeDirs: applyHeteroPermissionModeDir(
          preference.heteroPermissionModeDirs,
          key,
          mode,
        ),
      }),
      name: 'setHeteroPermissionModeDir',
      writePersonal: async () => ({
        heteroPermissionModeDirs: await userService.setHeteroPermissionModeDir(key, mode),
      }),
      writeWorkspace: () => workspaceUserSettingsService.setHeteroPermissionModeDir(key, mode),
    });

  /**
   * One per-directory habit of whoever is acting now: inside a workspace it
   * lives in this member's workspace bucket, in personal mode in
   * `users.preference`. Applied optimistically so the control changes at once,
   * rolled back if the write fails (rethrown, so the caller can say so); the
   * server applies the list's cap.
   */
  #setDirectoryPreference = async ({
    apply,
    name,
    writePersonal,
    writeWorkspace,
  }: {
    apply: (preference: DirectoryPreference) => DirectoryPreference;
    name: string;
    writePersonal: () => Promise<DirectoryPreference>;
    writeWorkspace: () => Promise<unknown>;
  }): Promise<void> => {
    const workspaceId = getActiveWorkspaceId();

    if (!workspaceId) {
      const previous = this.#get().preference;
      const optimistic = apply(previous);
      this.#set({ preference: { ...previous, ...optimistic } }, false, n(name));
      try {
        const stored = await writePersonal();
        this.#set(
          { preference: { ...this.#get().preference, ...stored } },
          false,
          n(`${name}/confirmed`),
        );
      } catch (error) {
        const rollback = Object.fromEntries(
          Object.keys(optimistic).map((field) => [
            field,
            previous[field as keyof DirectoryPreference],
          ]),
        );
        this.#set(
          { preference: { ...this.#get().preference, ...rollback } },
          false,
          n(`${name}/rollback`),
        );
        throw error;
      }
      return;
    }

    const replica = this.#replica();
    if (replica.getConfirmed(workspaceId) === undefined) {
      await replica.hydrate({ workspaceId });
      if (getActiveWorkspaceId() !== workspaceId) return;
    }
    const previous = this.#get().workspaceUserPreference;
    replica.update(workspaceId, (data) => data ?? previous, { persist: false });
    await replica.optimistic(workspaceId, (data) => ({ ...data, ...apply(data) }), writeWorkspace, {
      revalidate: true,
    });
  };
}

export type WorkspaceUserSettingsAction = Pick<
  WorkspaceUserSettingsActionImpl,
  keyof WorkspaceUserSettingsActionImpl
>;