Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

28.2 KB
Raw Blame
import { forgetTranscriptReadingState } from "../../lib/transcript-reading-position";
import i18n from "i18next";
import type { ProjectWorkspace } from "@pi-desktop/shared";
import { api } from "../../lib/api";
import { isRemoteSession } from "../../lib/remote-session-safety";
import {
  rememberProject,
  removeRecentProject,
  renameRecentProject,
  setProjectPinned,
} from "../../lib/recent-projects";
import {
  clearSessionPanes,
  releaseSessionPane,
} from "../../lib/session-panes";
import {
  clearSessionAsks,
  dropSessionAskDrafts,
} from "../../lib/pending-asks";
import {
  clearSessionPermissions,
} from "../../lib/pending-permissions";
import {
  projectIsArchived,
  projectIsCollapsed,
  projectIsPinned,
  sessionIsArchived,
  sessionIsPinned,
  sortProjects,
  sortSessions,
  normalizeProjectName,
  type ProjectMeta,
} from "../../lib/sidebar-preferences";
import {
  normalizeProjectPath,
  sessionMatchesProject,
} from "../../lib/sidebar-session-groups";
import type { AppState } from "../app-state";
import type { SessionRuntime } from "../runtime/session-runtime";
import type { StoreAccess } from "./types";

export type ProjectSliceDependencies = StoreAccess & {
  runtime: SessionRuntime;
  withoutRecordKey: <T>(record: Record<string, T>, key: string) => Record<string, T>;
  withProjectDisplayName: (
    workspace: ProjectWorkspace,
    projectMeta: Record<string, ProjectMeta>,
  ) => ProjectWorkspace;
  promoteProjectPath: (paths: string[], path: string) => string[];
  removeProjectPath: (paths: string[], path: string) => string[];
  upsertWorkspace: (
    projects: ProjectWorkspace[],
    workspace: ProjectWorkspace,
  ) => ProjectWorkspace[];
  persistCurrentSidebar: (getState: () => AppState) => void;
};

/**
 * Create one logical project group from already-resolved folders and activate
 * its primary root. The Create project dialog reaches this through a local
 * folder pick and through a git checkout, so both sources share one project
 * semantic and one activation path.
 */
async function createNamedProjectGroup(
  {
    get,
    set,
    runtime,
  }: Pick<ProjectSliceDependencies, "get" | "set" | "runtime">,
  {
    name,
    folders,
    primaryPath,
  }: { name: string; folders: string[]; primaryPath?: string },
): Promise<void> {
  const normalizedName = name.trim();
  if (!normalizedName) {
    throw new Error(i18n.t("errors.projectNameLength"));
  }
  const uniqueFolders = folders.filter(
    (path, index, all) =>
      Boolean(normalizeProjectPath(path)) &&
      all.findIndex(
        (candidate) => normalizeProjectPath(candidate) === normalizeProjectPath(path),
      ) === index,
  );
  if (uniqueFolders.length === 0) {
    throw new Error(i18n.t("project.createFolderRequired"));
  }
  const normalizedPrimary = normalizeProjectPath(primaryPath ?? uniqueFolders[0]);
  const primary =
    uniqueFolders.find((path) => normalizeProjectPath(path) === normalizedPrimary) ??
    uniqueFolders[0];
  const orderedFolders = [
    primary,
    ...uniqueFolders.filter(
      (path) => normalizeProjectPath(path) !== normalizeProjectPath(primary),
    ),
  ];
  const intent = runtime.beginNavigationIntent();
  // Closing a project only removes it from the sidebar. Its host-owned group
  // remains durable, so choosing that one folder again means reopen the group
  // instead of trying to create a duplicate owner for the same path.
  if (orderedFolders.length === 1) {
    const { groups } = await api.listProjectGroups();
    if (!runtime.navigationIntentIsCurrent(intent)) return;
    const selectedPath = normalizeProjectPath(orderedFolders[0]);
    const existing = groups.find((group) =>
      group.roots.some((root) => normalizeProjectPath(root.path) === selectedPath),
    );
    if (existing) {
      const existingPrimary = existing.primaryPath || orderedFolders[0];
      const workspace = await get().activateProject(existingPrimary, {
        navigationIntent: intent,
      });
      if (!workspace || !runtime.navigationIntentIsCurrent(intent)) return;
      const onboarding = await api.getOnboarding();
      if (!runtime.navigationIntentIsCurrent(intent)) return;
      set({ createProjectDialogOpen: false, createProjectDialogRemoteHostKey: undefined, onboarding, page: "chat", settingsOpen: false });
      return;
    }
  }
  const created = await api.createProjectGroup(normalizedName, orderedFolders);
  if (!runtime.navigationIntentIsCurrent(intent)) return;
  const groupPrimary = created.group.primaryPath || primary;
  const workspace = await get().activateProject(groupPrimary, {
    navigationIntent: intent,
  });
  if (!workspace || !runtime.navigationIntentIsCurrent(intent)) return;
  // Keep the existing renderer-local metadata in sync so the sidebar can
  // render the group name immediately; the host group is authoritative on
  // the next archive refresh and for agent context.
  get().renameProject(groupPrimary, normalizedName);
  const onboarding = await api.getOnboarding();
  if (!runtime.navigationIntentIsCurrent(intent)) return;
  set({ createProjectDialogOpen: false, createProjectDialogRemoteHostKey: undefined, onboarding, page: "chat", settingsOpen: false });
}

/**
 * Purge renderer-local state for one session whose durable row is already gone
 * (deleted directly, or removed together with its project). This never talks to
 * the host: records and transcripts are deleted before it runs.
 */
function clearLocalSessionState(
  {
    get,
    set,
    runtime,
    withoutRecordKey,
  }: Pick<
    ProjectSliceDependencies,
    "get" | "set" | "runtime" | "withoutRecordKey"
  >,
  id: string,
): void {
  forgetTranscriptReadingState(id);
  runtime.pendingSessionConfigurations.delete(id);
  runtime.sessionTranscriptCache.delete(id);
  runtime.sessionHistoryCache.delete(id);
  runtime.liveSessionTranscripts.delete(id);
  if (get().activeSessionId === id) get().resetWorkPanelContext();
  set((state) => {
    const sessionMeta = { ...state.sessionMeta };
    delete sessionMeta[id];
    const sessions = state.sessions.filter((session) => session.id !== id);
    const runningSessions = { ...state.runningSessions };
    delete runningSessions[id];
    const agentStatuses = { ...state.agentStatuses };
    delete agentStatuses[id];
    const sessionOutcomes = { ...state.sessionOutcomes };
    delete sessionOutcomes[id];
    const queuedPrompts = withoutRecordKey(state.queuedPrompts, id);
    const pendingPermissions = clearSessionPermissions(
      state.pendingPermissions,
      id,
    );
    const pendingAsks = clearSessionAsks(state.pendingAsks, id);
    const askDrafts = dropSessionAskDrafts(state.askDrafts, state.pendingAsks, id);
    const latestTurnResults = withoutRecordKey(state.latestTurnResults, id);
    const planningStates = withoutRecordKey(state.planningStates, id);
    const pendingPlans = withoutRecordKey(state.pendingPlans, id);
    const planCheckpoints = withoutRecordKey(state.planCheckpoints, id);
    const sessionCompactions = withoutRecordKey(state.sessionCompactions, id);
    const sessionHistory = withoutRecordKey(state.sessionHistory, id);
    const retainedNav = state.navStack.filter(
      (entry) => entry.sessionId !== id,
    );
    const navStack =
      retainedNav.length > 0 ? retainedNav : [{ page: "chat" as const }];
    return {
      ...releaseSessionPane(state, id),
      sessionMeta,
      sessions,
      runningSessions,
      agentStatuses,
      sessionOutcomes,
      queuedPrompts,
      workPanelContexts: withoutRecordKey(state.workPanelContexts, id),
      fileManagerTabReceipts: withoutRecordKey(state.fileManagerTabReceipts, id),
      activeSessionId:
        state.activeSessionId === id ? undefined : state.activeSessionId,
      selectingSessionId:
        state.selectingSessionId === id ? undefined : state.selectingSessionId,
      messages: state.activeSessionId === id ? [] : state.messages,
      isRunning: state.activeSessionId === id ? false : state.isRunning,
      pendingPermissions,
      pendingAsks,
      askDrafts,
      latestTurnResults,
      planningStates,
      pendingPlans,
      planCheckpoints,
      sessionCompactions,
      sessionHistory,
      navStack,
      navIndex: Math.min(state.navIndex, navStack.length - 1),
    };
  });
}

export function createProjectSlice({
  get,
  set,
  runtime,
  withoutRecordKey,
  withProjectDisplayName,
  promoteProjectPath,
  removeProjectPath,
  upsertWorkspace,
  persistCurrentSidebar,
}: ProjectSliceDependencies): Pick<
  AppState,
  | "activateProject"
  | "refreshProject"
  | "openProjectPath"
  | "switchProjectPath"
  | "closeProjectPath"
  | "closeProject"
  | "cloneProject"
  | "openProject"
  | "closeProjectDialog"
  | "createProjectFromFolders"
  | "createProjectFromRemote"
  | "createProjectFromGit"
  | "clearProject"
  | "deleteProject"
  | "toggleSessionPinned"
  | "toggleSessionArchived"
  | "archiveSession"
  | "restoreSession"
  | "renameSession"
  | "moveSessionProject"
  | "deleteSession"
  | "setSessionSort"
  | "setSessionArchiveVisibility"
  | "setSessionView"
  | "setShowArchived"
  | "archiveProject"
  | "toggleProjectPinned"
  | "renameProject"
  | "toggleProjectArchived"
  | "restoreProject"
  | "restoreProjects"
  | "setProjectCollapsed"
  | "toggleProjectCollapsed"
  | "setProjectSort"
  | "reorderProjects"
  | "getVisibleSessions"
  | "getSortedProjects"
> {
  return {
    activateProject: async (path, opts) => {
      const intent = opts?.navigationIntent ?? runtime.beginNavigationIntent();
      const preserveConversation = runtime.isSessionSelectionForIntent(intent);
      const requestedPath = path.trim();
      if (!requestedPath) return null;
      const result = await api.setProject(requestedPath);
      if (!runtime.navigationIntentIsCurrent(intent)) return null;
      const workspace = result.workspace
        ? withProjectDisplayName(result.workspace, get().projectMeta)
        : null;
      if (!workspace?.path) return null;
      if (
        normalizeProjectPath(get().activeProjectPath) !==
          normalizeProjectPath(workspace.path) &&
        !preserveConversation
      ) {
        get().resetWorkPanelContext();
      }

      set((state) => {
        const switchesVisibleProject =
          normalizeProjectPath(state.activeProjectPath) !==
          normalizeProjectPath(workspace.path);
        const openProjectPaths = promoteProjectPath(
          state.openProjectPaths,
          workspace.path,
        );
        const openProjects = upsertWorkspace(state.openProjects, workspace);
        return {
          workspace,
          activeProjectPath: workspace.path,
          openProjectPaths,
          openProjects,
          page: "chat" as const,
          ...(switchesVisibleProject && !preserveConversation
            ? {
                ...clearSessionPanes(),
                activeSessionId: undefined,
                messages: [],
                isRunning: false,
              }
            : {}),
        };
      });
      rememberProject({
        ...(workspace.remote ? { remote: workspace.remote } : {}),
        path: workspace.path,
        name: workspace.name || workspace.path,
        branch: workspace.branch,
      });
      if (projectIsArchived(workspace.path, get().projectMeta)) {
        get().restoreProject(workspace.path);
      } else {
        persistCurrentSidebar(get);
      }
      return workspace;
    },

    refreshProject: async (path) => {
      const requestedKey = normalizeProjectPath(path);
      if (!requestedKey) return null;

      const result = await api.getProject();
      const workspace = result.workspace
        ? withProjectDisplayName(result.workspace, get().projectMeta)
        : null;
      if (
        !workspace?.path ||
        normalizeProjectPath(workspace.path) !== requestedKey
      ) {
        return null;
      }

      let applied = false;
      set((state) => {
        if (normalizeProjectPath(state.activeProjectPath) !== requestedKey) {
          return state;
        }
        applied = true;
        return {
          workspace,
          openProjects: upsertWorkspace(state.openProjects, workspace),
        };
      });
      return applied ? workspace : null;
    },

    openProjectPath: async (path) => get().activateProject(path),
    switchProjectPath: async (path) => get().activateProject(path),

    closeProjectPath: async (path) => {
      const intent = runtime.beginNavigationIntent();
      const key = normalizeProjectPath(path);
      if (!key) return;
      const state = get();
      const isActive = normalizeProjectPath(state.activeProjectPath) === key;
      const nextPaths = removeProjectPath(state.openProjectPaths, path);
      if (isActive) {
        const fallbackPath = nextPaths[nextPaths.length - 1];
        if (fallbackPath) {
          await get().activateProject(fallbackPath, { navigationIntent: intent });
        } else {
          await get().clearProject({ navigationIntent: intent });
        }
        if (!runtime.navigationIntentIsCurrent(intent)) return;
      }
      set((current) => ({
        openProjectPaths: removeProjectPath(current.openProjectPaths, path),
        openProjects: current.openProjects.filter(
          (project) => normalizeProjectPath(project.path) !== key,
        ),
      }));
      persistCurrentSidebar(get);
    },
    closeProject: async (path) => get().closeProjectPath(path),

    cloneProject: async (url) => {
      const intent = runtime.beginNavigationIntent();
      const result = await api.cloneProject(url);
      if (!runtime.navigationIntentIsCurrent(intent)) return null;
      if (result.canceled || !result.workspace?.path) return null;
      return get().activateProject(result.workspace.path, {
        navigationIntent: intent,
      });
    },

    openProject: async (options) => {
      set({ createProjectDialogOpen: true, createProjectDialogRemoteHostKey: options?.remoteHostKey });
    },
    closeProjectDialog: () => {
      set({ createProjectDialogOpen: false, createProjectDialogRemoteHostKey: undefined });
    },
    createProjectFromFolders: async ({ name, folders, primaryPath }) =>
      createNamedProjectGroup(
        { get, set, runtime },
        { name, folders, primaryPath },
      ),
    createProjectFromRemote: async ({ name, hostKey, path }) => {
      const normalizedName = normalizeProjectName(name);
      if (!normalizedName) throw new Error(i18n.t("errors.projectNameLength"));
      const intent = runtime.beginNavigationIntent();
      const { group } = await api.createRemoteProjectGroup(normalizedName, hostKey, path);
      if (!runtime.navigationIntentIsCurrent(intent)) return;
      const workspace = await get().activateProject(group.primaryPath, { navigationIntent: intent });
      if (!workspace || !runtime.navigationIntentIsCurrent(intent)) return;
      get().renameProject(group.primaryPath, normalizedName);
      const onboarding = await api.getOnboarding();
      if (!runtime.navigationIntentIsCurrent(intent)) return;
      set({ createProjectDialogOpen: false, createProjectDialogRemoteHostKey: undefined, onboarding, page: "chat", settingsOpen: false });
    },
    createProjectFromGit: async ({ name, url, parentPath }) => {
      // Clone first, then create the project through the same group path, so a
      // checkout and a folder pick produce identical project semantics.
      const checkout = await api.cloneProjectInto(url, parentPath);
      await createNamedProjectGroup(
        { get, set, runtime },
        { name, folders: [checkout.path], primaryPath: checkout.path },
      );
    },

    clearProject: async (opts) => {
      const intent = opts?.navigationIntent ?? runtime.beginNavigationIntent();
      const preserveConversation = runtime.isSessionSelectionForIntent(intent);
      await api.clearProject();
      if (!runtime.navigationIntentIsCurrent(intent)) return;
      if (!preserveConversation) get().resetWorkPanelContext();
      set({
        workspace: null,
        activeProjectPath: undefined,
        ...(preserveConversation
          ? {}
          : {
              ...clearSessionPanes(),
              activeSessionId: undefined,
              messages: [],
              isRunning: false,
            }),
      });
      persistCurrentSidebar(get);
      const onboarding = await api.getOnboarding();
      if (!runtime.navigationIntentIsCurrent(intent)) return;
      set({ onboarding });
    },

    deleteProject: async (path) => {
      const key = normalizeProjectPath(path);
      if (!key) return;
      const removedSessionIds = get()
        .sessions.filter(
          (session) => normalizeProjectPath(session.projectPath) === key,
        )
        .map((session) => session.id);
      // A path the host has no durable row for is not a failure: the local
      // records cleared below are the only thing that can keep such a row
      // visible, so an already-removed project still leaves the desktop.
      await api.removeProject(path);
      for (const id of removedSessionIds) {
        clearLocalSessionState(
          { get, set, runtime, withoutRecordKey },
          id,
        );
      }
      set((state) => {
        const projectMeta = { ...state.projectMeta };
        delete projectMeta[key];
        return { projectMeta };
      });
      try {
        removeRecentProject(path);
      } catch {
        // Recent projects are a best-effort renderer cache.
      }
      const isOpen =
        normalizeProjectPath(get().activeProjectPath) === key ||
        get().openProjectPaths.some(
          (openPath) => normalizeProjectPath(openPath) === key,
        );
      if (isOpen) await get().closeProjectPath(path);
      persistCurrentSidebar(get);
      await get().refreshSessions();
    },

    toggleSessionPinned: (id) => {
      if (!id) return;
      set((state) => {
        const pinned = !sessionIsPinned(id, state.sessionMeta);
        const sessionMeta = {
          ...state.sessionMeta,
          [id]: { ...(state.sessionMeta[id] || {}), pinned },
        };
        const sessions = state.sessions.map((session) =>
          session.id === id ? { ...session, pinned } : session,
        );
        return { sessionMeta, sessions };
      });
      persistCurrentSidebar(get);
    },

    toggleSessionArchived: (id) => {
      if (!id) return;
      set((state) => {
        const archived = !sessionIsArchived(id, state.sessionMeta);
        const sessionMeta = {
          ...state.sessionMeta,
          [id]: { ...(state.sessionMeta[id] || {}), archived },
        };
        const sessions = state.sessions.map((session) =>
          session.id === id ? { ...session, archived } : session,
        );
        return { sessionMeta, sessions };
      });
      persistCurrentSidebar(get);
    },

    archiveSession: (id) => {
      if (!id) return;
      set((state) => ({
        sessionMeta: {
          ...state.sessionMeta,
          [id]: { ...(state.sessionMeta[id] || {}), archived: true },
        },
        sessions: state.sessions.map((session) =>
          session.id === id ? { ...session, archived: true } : session,
        ),
      }));
      persistCurrentSidebar(get);
    },

    restoreSession: (id) => {
      if (!id) return;
      set((state) => ({
        sessionMeta: {
          ...state.sessionMeta,
          [id]: { ...(state.sessionMeta[id] || {}), archived: false },
        },
        sessions: state.sessions.map((session) =>
          session.id === id ? { ...session, archived: false } : session,
        ),
      }));
      persistCurrentSidebar(get);
    },

    renameSession: async (id, title) => {
      if (!id) return;
      const nextTitle = title.trim();
      if (!nextTitle) throw new Error(i18n.t("errors.sessionTitleEmpty"));
      const result = await api.renameSession(id, nextTitle);
      if (!result.ok) throw new Error(i18n.t("errors.sessionNotFound"));
      set((state) => ({
        sessions: state.sessions.map((session) =>
          session.id === id ? { ...session, title: nextTitle } : session,
        ),
      }));
      persistCurrentSidebar(get);
    },

    moveSessionProject: async (id, projectPath) => {
      const state = get();
      const session = state.sessions.find((item) => item.id === id);
      if (!id || !session || session.source === "pi-native" || isRemoteSession(session)) return false;
      if (state.runningSessions[id]) return false;
      const destinationKey = projectPath === null ? null : normalizeProjectPath(projectPath);
      if (projectPath !== null && !destinationKey) return false;
      if ((normalizeProjectPath(session.projectPath) || null) === destinationKey) return true;
      if (
        destinationKey !== null &&
        !state.openProjectPaths.some(
          (path) => normalizeProjectPath(path) === destinationKey,
        )
      ) {
        return false;
      }
      const result = await api.moveSessionProject(id, projectPath);
      set((current) => ({
        sessions: current.sessions.map((item) =>
          item.id === id ? { ...item, ...result.session, projectPath: result.session.projectPath ?? undefined } : item,
        ),
      }));
      // The active session decides the visible project: follow it so the
      // composer, file tree, and project instructions match the new owner.
      // Re-selecting aligns the workspace while keeping the conversation.
      if (get().activeSessionId === id) await get().selectSession(id, { record: false });
      return true;
    },

    deleteSession: async (id) => {
      if (!id) return;
      try {
        await api.deleteSession(id);
      } catch (error) {
        // The main-side running guard is the backstop for races the sidebar's
        // pre-check cannot see (issue #2291); surface it translated instead
        // of the raw IPC error.
        if (error instanceof Error && error.message.includes("Cannot delete a running session")) {
          get().showToast(i18n.t("nav.deleteRunningSessionBlocked"), { variant: "warning" });
          return;
        }
        throw error;
      }
      void api.pluginViewClose("pi.browser", "browser", { sessionId: id })
        .catch((error) => get().showToast(String(error), { variant: "error" }));
      clearLocalSessionState({ get, set, runtime, withoutRecordKey }, id);
      persistCurrentSidebar(get);
      await get().refreshSessions();
    },

    setSessionSort: (sort) => {
      set((state) => ({
        sessionView: { ...state.sessionView, sort, sortBy: sort },
      }));
      persistCurrentSidebar(get);
    },

    setSessionArchiveVisibility: (show) => {
      set((state) => ({
        sessionView: { ...state.sessionView, archived: show, showArchived: show },
      }));
      persistCurrentSidebar(get);
    },

    setSessionView: (view) => {
      if (typeof view === "boolean") {
        get().setSessionArchiveVisibility(view);
        return;
      }
      if (view.sort || view.sortBy) {
        get().setSessionSort(view.sort ?? view.sortBy ?? get().sessionView.sort);
      }
      if (view.archived !== undefined || view.showArchived !== undefined) {
        get().setSessionArchiveVisibility(
          view.archived ?? view.showArchived ?? false,
        );
      }
    },

    setShowArchived: (show) => {
      get().setSessionArchiveVisibility(show);
    },

    archiveProject: (path) => {
      const key = normalizeProjectPath(path);
      if (!key) return;
      set((state) => ({
        projectMeta: {
          ...state.projectMeta,
          [key]: { ...(state.projectMeta[key] || {}), archived: true },
        },
      }));
      persistCurrentSidebar(get);
    },

    toggleProjectPinned: (path, requestedPinned) => {
      const key = normalizeProjectPath(path);
      if (!key) return;
      set((state) => {
        const pinned = requestedPinned ?? !projectIsPinned(key, state.projectMeta);
        return {
          projectMeta: {
            ...state.projectMeta,
            [key]: { ...(state.projectMeta[key] || {}), pinned },
          },
        };
      });
      try {
        setProjectPinned(path, projectIsPinned(key, get().projectMeta));
      } catch {
        // The durable recent-project index is optional in restricted contexts.
      }
      persistCurrentSidebar(get);
    },

    renameProject: (path, name) => {
      const key = normalizeProjectPath(path);
      if (!key) return;
      const normalizedName = normalizeProjectName(name);
      if (!normalizedName) {
        throw new Error(i18n.t("errors.projectNameLength"));
      }
      set((state) => ({
        projectMeta: {
          ...state.projectMeta,
          [key]: { ...(state.projectMeta[key] || {}), name: normalizedName },
        },
        openProjects: state.openProjects.map((project) =>
          normalizeProjectPath(project.path) === key
            ? { ...project, name: normalizedName }
            : project,
        ),
        workspace:
          state.workspace && normalizeProjectPath(state.workspace.path) === key
            ? { ...state.workspace, name: normalizedName }
            : state.workspace,
      }));
      try {
        renameRecentProject(path, normalizedName);
      } catch {
        // Recent projects are a best-effort renderer cache.
      }
      persistCurrentSidebar(get);
    },

    toggleProjectArchived: (path) => {
      const key = normalizeProjectPath(path);
      if (!key) return;
      set((state) => {
        const archived = !projectIsArchived(key, state.projectMeta);
        return {
          projectMeta: {
            ...state.projectMeta,
            [key]: { ...(state.projectMeta[key] || {}), archived },
          },
        };
      });
      persistCurrentSidebar(get);
    },

    restoreProject: (path) => {
      const key = normalizeProjectPath(path);
      if (!key) return;
      set((state) => ({
        projectMeta: {
          ...state.projectMeta,
          [key]: { ...(state.projectMeta[key] || {}), archived: false },
        },
      }));
      persistCurrentSidebar(get);
    },

    restoreProjects: (paths) => {
      const keys = new Set<string>();
      for (const path of paths) {
        const key = normalizeProjectPath(path);
        if (key) keys.add(key);
      }
      const archivedKeys = [...keys].filter((key) =>
        projectIsArchived(key, get().projectMeta),
      );
      if (archivedKeys.length === 0) return;
      set((state) => {
        const projectMeta = { ...state.projectMeta };
        for (const key of archivedKeys) {
          projectMeta[key] = { ...(projectMeta[key] || {}), archived: false };
        }
        return { projectMeta };
      });
      persistCurrentSidebar(get);
    },

    setProjectCollapsed: (path, collapsed) => {
      const key = normalizeProjectPath(path);
      if (!key) return;
      set((state) => {
        const next = collapsed ?? !projectIsCollapsed(key, state.projectMeta);
        return {
          projectCollapsed: { ...state.projectCollapsed, [key]: next },
          projectMeta: {
            ...state.projectMeta,
            [key]: { ...(state.projectMeta[key] || {}), collapsed: next },
          },
        };
      });
      persistCurrentSidebar(get);
    },

    toggleProjectCollapsed: (path) => get().setProjectCollapsed(path),

    setProjectSort: (sort) => {
      set({ projectSort: sort });
      persistCurrentSidebar(get);
    },

    reorderProjects: (paths) => {
      const orderedKeys: string[] = [];
      const seen = new Set<string>();
      for (const path of paths) {
        const key = normalizeProjectPath(path);
        if (!key || seen.has(key)) continue;
        seen.add(key);
        orderedKeys.push(key);
      }
      if (orderedKeys.length < 2) return;
      set((state) => {
        const projectMeta = { ...state.projectMeta };
        orderedKeys.forEach((key, index) => {
          projectMeta[key] = { ...(projectMeta[key] || {}), order: index };
        });
        return { projectMeta, projectSort: "manual" };
      });
      persistCurrentSidebar(get);
    },

    getVisibleSessions: (options) => {
      const state = get();
      const includeArchived = options?.includeArchived ?? state.sessionView.archived;
      const scoped =
        options && "projectPath" in options
          ? state.sessions.filter((session) =>
              sessionMatchesProject(session, options.projectPath),
            )
          : state.sessions;
      return sortSessions(
        scoped,
        state.sessionMeta,
        state.sessionView.sort,
        includeArchived,
      );
    },

    getSortedProjects: () => {
      const state = get();
      const projects = state.openProjects.filter(
        (project) => !projectIsArchived(project.path, state.projectMeta),
      );
      return sortProjects(projects, state.projectMeta, state.projectSort);
    },
  };
}