Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

4.3 KB
Raw Blame
import { openPluginTaskTab } from './openPluginTaskTab';
import { openRoutinesTab } from './openRoutinesTab';
import { openMailPreview, openMailCard } from './mailPreview';
/** 执行 main 已裁决并推给当前 renderer host 的 RSB command。 */

import type { RsbWindowCommand } from '../../../../shared/rightSidebarWindow';
import { addOrFocusSingletonTab, closeTab, ensureHydrated, getBucket } from '../store';
import {
  closeOrcaWorkersTabAfterTeamEnd,
  ensureOrcaWorkersTab,
} from '../plugins/orca-workers/actions';
import {
  openDirInSidebarFileBrowser,
  openExternalFileInSidebarFileBrowser,
  openFileInSidebarFileBrowser,
} from './openInSidebarFileBrowser';
import { openBackgroundTasksTab } from './openBackgroundTasksTab';
import { openSubagentsTab } from './openSubagentsTab';
import { openTurnReview } from './openTurnReview';
import { openUrlInSidebarBrowser } from './openInSidebarBrowser';

/** 在 main 已选定的当前 renderer host 中执行命令,不自行选择宿主。 */
export async function executeSidebarCommand(command: RsbWindowCommand): Promise<void> {
  if (command.type === 'open-mail-card') {
    await openMailCard(command.sessionId, command.callId);
    return;
  }
  if (command.type === 'open-mail-preview') {
    await openMailPreview(command.sessionId, command.url);
    return;
  }
  if (command.type === 'open-plugin-task') {
    await openPluginTaskTab(command.sessionId, command.instanceId, {
      userInitiated: command.userInitiated === true,
    });
    return;
  }
  if (command.type === 'open-routines-tab') {
    await openRoutinesTab(command.sessionId, command.botId);
    return;
  }
  if (command.type === 'open-web-browser') {
    await openUrlInSidebarBrowser(command.sessionId, command.url);
    return;
  }
  if (command.type === 'open-file-browser') {
    if (command.targetKind === 'external-file') {
      await openExternalFileInSidebarFileBrowser(command.sessionId, command.absPath);
    } else if (command.targetKind === 'directory') {
      await openDirInSidebarFileBrowser(command.sessionId, command.relPath);
    } else {
      await openFileInSidebarFileBrowser(command.sessionId, command.relPath);
    }
    return;
  }
  if (command.type === 'ensure-orca-workers-tab') {
    await ensureOrcaWorkersTab(command.sessionId, {
      focusWorkerSessionId: command.focusWorkerSessionId,
      searchJump: command.searchJump,
      focusTab: command.focusTab === true,
    });
    return;
  }
  if (command.type === 'close-orca-workers-tab') {
    await closeOrcaWorkersTabAfterTeamEnd(command.sessionId);
    return;
  }
  if (command.type === 'open-background-tasks-tab') {
    // 被路由端再调 openBackgroundTasksTab:其内部 routeSidebarCommand 在子窗口
    // 直接裁决 'attached',落本地 store(与 open-file-browser 的复用同构)。
    await openBackgroundTasksTab(command.sessionId, {
      ...(command.focusTaskId ? { focusTaskId: command.focusTaskId } : {}),
    });
    return;
  }
  if (command.type === 'open-subagents-tab') {
    await openSubagentsTab(command.sessionId, {
      ...(command.focusRunId && command.focusProvider
        ? { focusRunId: command.focusRunId, focusProvider: command.focusProvider }
        : {}),
      focusTab: command.focusTab !== false,
      revealSidebar: command.revealSidebar !== false,
      userInitiated: false,
    });
    return;
  }
  if (command.type === 'open-turn-review') {
    await openTurnReview(command.sessionId, command.changeSetIds, {
      selectedDiffId: command.selectedDiffId ?? null,
      selectedPath: command.selectedPath ?? null,
      requestNonce: command.requestNonce,
      hostSessionId: command.hostSessionId ?? null,
    });
    return;
  }
  if (command.type === 'toggle-review-tab') {
    await ensureHydrated(command.sessionId);
    const bucket = getBucket(command.sessionId);
    const reviewTab = bucket.tabs.find((tab) => tab.kind === 'review');
    const hostAlreadyVisible = typeof document === 'undefined' || document.visibilityState === 'visible';
    if (reviewTab && bucket.activeTabId === reviewTab.id && hostAlreadyVisible) {
      await closeTab(command.sessionId, reviewTab.id);
      return;
    }
    await addOrFocusSingletonTab(command.sessionId, 'review', null);
    return;
  }
  await ensureHydrated(command.sessionId);
  await addOrFocusSingletonTab(command.sessionId, 'terminal');
}